132

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`
1

2 Answers 2

196

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"));
}
3
  • 2
    @Juxhin it's often the simple answers that are hardest to find! ^_^
    – Shepmaster
    Sep 3, 2015 at 20:22
  • Indeed. I would understand scenarios where you would want to do more than a validation check but since I'm just getting myself used to language for now I wanted to just slowly expose myself to the standard API as I've never delved into this programming paradigm before. Thanks again :-)
    – Juxhin
    Sep 3, 2015 at 20:24
  • 10
    It is worth mentioning that Path::exists is an alias to fs::metadata(path).is_ok()
    – mental
    Jan 26, 2019 at 16:15
12

You can use std::path::Path::is_file:

use std::path::Path;

fn main() {
   let b = Path::new("file.txt").is_file();
   println!("{}", b);
}

https://doc.rust-lang.org/std/path/struct.Path.html#method.is_file

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.