Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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?

like image 990
xophos Avatar asked Nov 09 '22 09:11

xophos


1 Answers

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).

like image 56
Chris Morgan Avatar answered Jan 04 '23 03:01

Chris Morgan