Background
Determining if a file exists is a oneliner in Rust.
Code
Outline
Struct std::path::Path
- A slice of a path (akin to str).
- This type supports a number of operations for inspecting a path, including breaking the path into its components (separated by / on Unix and by either / or \ on Windows), extracting the file name, determining whether the path is absolute, and so on.
- This is an unsized type, meaning that it must always be used behind a pointer like & or Box.
- For an owned version of this type, see PathBuf.
Source Code
Code Source
/* declare pathfile as structure std::path::Path */ let pathfile:&std::path::Path; //filename let filename:&str; //file exists let mut b_file_exist:bool; //set filename //filename = "....."; /* instanciate new path object for filename */ pathfile = std::path::Path::new(filename); //does file exist b_file_exist = pathfile.exists(); if b_file_exist == false { println!( "File {0} does not exist! (API is path::new::exists)!" , filename ); }