简体   繁体   English

如何检查Rust 1.1中是否存在路径?

[英]How to check the existence of a path in Rust 1.1?

In Rust 1.1, std::fs::PathExt is marked unstable; 在Rust 1.1中, std::fs::PathExt标记为不稳定; how do I check the existence of a file or directory? 如何检查文件或目录的存在?

Is there a canonical solution for this or do i have to read the source of std::fs::PathExt ? 有没有规范的解决方案,或者我必须阅读std::fs::PathExt

Is there maybe a crate that delivers this functionality? 是否有可能提供此功能的箱子?

PathExt is simple wrappers around std::fs::metadata ; PathExtstd::fs::metadata简单包装器; if the path doesn't exist, metadata will return an error, so PathExt.exists() is a simple metadata(self).is_ok() . 如果路径不存在, metadata将返回错误,因此PathExt.exists()是一个简单的metadata(self).is_ok()

Typically you should be using is_file or is_dir instead, though; 通常,您应该使用is_fileis_dir ; they correspond to metadata(self).map(|m| m. «is_file or is_dir» ()).unwrap_or(false) . 它们对应于metadata(self).map(|m| m. «is_file or is_dir» ()).unwrap_or(false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM