ADVERTISEMENT
ADVERTISEMENT

Rust program to check a specified file exists or not

Rust | File I/O Example: Write a program to check a specified file exists or not.
Submitted by Nidhi, on October 31, 2021

Problem Solution:

In this program, we will check whether a specified exists or not using exists() method and The exists() method returns a boolean value.

Program/Source Code:

The source code to check a specified file exists or not is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to check a specified 
// file exists or not

use std::path::Path;

fn main() {
    let mut rs:bool=true;
    
    rs = Path::new("empty.txt").exists();
    
    if rs == true{
        println!("File exists");
    }
    else{
        println!("File does not exist");
    }  
}

Output:

$ rustc main.rs

$ ./main
File exists

Explanation:

Here, we checked the "empty.txt" file is exists or not using exists() method. The exists() method returns Boolean value. It returns true if the file exists, otherwise, it returns false. After that, we printed the appropriate message.

Rust File I/O Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.