Rust:- File I/O – Does File Exists?


Determining if a file exists is a oneliner in Rust.



Struct std::path::Path


  1. A slice of a path (akin to str).
  2. 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.
  3. This is an unsized type, meaning that it must always be used behind a pointer like & or Box.
  4. 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;

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
		     "File {0} does not exist! (API is path::new::exists)!"
		   , filename



  1. Module
  2. Structure
    • std::path::Path

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s