June 2023

Check if a File or Directory Path Exists in Rust

use std::path::PathBuf;

fn main() {
    let path = PathBuf::from("files/alfa.txt");
    let exists = path.try_exists().unwrap_or(false);
    println!("Exists: {}", exists);
}
Output:
Exists: true

The is the basic way to check if a file exists using `.try_exists()`rust` instead of `.exists()`rust`. The difference between the two is that `.try_exists()rust will produce an Err for things like permissions issues or broken symbolic links.

(NOTE: Need to look into that a little more to confirm the differences)

end of line