简体   繁体   中英

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

In Rust 1.1, std::fs::PathExt is marked unstable; 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 ?

Is there maybe a crate that delivers this functionality?

PathExt is simple wrappers around std::fs::metadata ; if the path doesn't exist, metadata will return an error, so PathExt.exists() is a simple metadata(self).is_ok() .

Typically you should be using is_file or is_dir instead, though; they correspond to metadata(self).map(|m| m. «is_file or is_dir» ()).unwrap_or(false) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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