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

Background

Determining if a file exists is a oneliner in Rust.

Code

Outline

Struct std::path::Path

Link

  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;

//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
		);     
}
		

 

References

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

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s