如何检查路径是否存在? [英] How to check whether a path exists?
问题描述
选择似乎是在std::fs::PathExt
和std::fs::metadata
之间,但暂时建议使用后者更稳定.以下是我一直在使用的代码,因为它基于文档:
使用 std::fs;pub fn path_exists(path: &str) ->布尔{let metadata = try!(fs::metadata(path));assert!(metadata.is_file());}
然而,由于一些奇怪的原因 let metadata = try!(fs::metadata(path))
仍然需要函数返回一个 Result
即使我只想返回一个布尔值,如 assert!(metadata.is_file())
所示.
即使很快就会对此进行大量更改,我该如何绕过 try!()
问题?
以下是相关的编译器错误:
错误[E0308]:类型不匹配-->src/main.rs:4:20|4 |let metadata = try!(fs::metadata(path));|^^^^^^^^^^^^^^^^^^^^^^^^ 预期布尔值,找到枚举`std::result::Result`|= 注意:预期类型`bool`找到类型`std::result::Result<_, _>`= 注意:此错误源自当前 crate 之外的宏错误[E0308]:类型不匹配-->src/main.rs:3:40|3 |pub fn path_exists(path: &str) ->布尔{|________________________________________^4 ||let metadata = try!(fs::metadata(path));5 ||assert!(metadata.is_file());6 ||}||_^ 预期 (), 发现 bool|= 注意:预期类型`()`找到类型`bool`
请注意,很多时候您想对文件做某事,例如阅读它.在这些情况下,尝试打开它并处理 Result
更有意义.这消除了检查文件是否存在"和打开"之间的竞争条件文件(如果存在)".如果你真正关心的是它是否存在......
Rust 1.5+
Path::exists
... 存在:
使用 std::path::Path;fn 主(){println!("{}", Path::new("/etc/hosts").exists());}
心理指出, Path::exists
只需 为你调用fs::metadata
:
pub fn exists(&self) ->布尔{fs::metadata(self).is_ok()}
Rust 1.0+
您可以检查 fs::metadata代码>
方法成功:
使用 std::fs;pub fn path_exists(path: &str) ->布尔{fs::metadata(path).is_ok()}fn 主(){println!("{}", path_exists("/etc/hosts"));}
The choice seems to be between std::fs::PathExt
and std::fs::metadata
, but the latter is suggested for the time being as it is more stable. Below is the code I have been working with as it is based off the docs:
use std::fs;
pub fn path_exists(path: &str) -> bool {
let metadata = try!(fs::metadata(path));
assert!(metadata.is_file());
}
However, for some odd reason let metadata = try!(fs::metadata(path))
still requires the function to return a Result<T,E>
even though I simply want to return a boolean as shown from assert!(metadata.is_file())
.
Even though there will probably be a lot of changes to this soon enough, how would I bypass the try!()
issue?
Below is the relevant compiler error:
error[E0308]: mismatched types
--> src/main.rs:4:20
|
4 | let metadata = try!(fs::metadata(path));
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected bool, found enum `std::result::Result`
|
= note: expected type `bool`
found type `std::result::Result<_, _>`
= note: this error originates in a macro outside of the current crate
error[E0308]: mismatched types
--> src/main.rs:3:40
|
3 | pub fn path_exists(path: &str) -> bool {
| ________________________________________^
4 | | let metadata = try!(fs::metadata(path));
5 | | assert!(metadata.is_file());
6 | | }
| |_^ expected (), found bool
|
= note: expected type `()`
found type `bool`
Note that many times you want to do something with the file, like read it. In those cases, it makes more sense to just try to open it and deal with the Result
. This eliminates a race condition between "check to see if file exists" and "open file if it exists". If all you really care about is if it exists...
Rust 1.5+
Path::exists
... exists:
use std::path::Path;
fn main() {
println!("{}", Path::new("/etc/hosts").exists());
}
As mental points out, Path::exists
simply calls fs::metadata
for you:
pub fn exists(&self) -> bool { fs::metadata(self).is_ok() }
Rust 1.0+
You can check if the fs::metadata
method succeeds:
use std::fs;
pub fn path_exists(path: &str) -> bool {
fs::metadata(path).is_ok()
}
fn main() {
println!("{}", path_exists("/etc/hosts"));
}
这篇关于如何检查路径是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!