Before performing any file operations within a program, it is good to check and ensure that a file exists before attempting to read, write, copy, or move it. This provides a fundamental error handling mechanism which prevents the program execution from terminating in case of a missing file.
In this tutorial, we will learn how to use Rust’s built-in features to check and verify that a given file exists within the specified path before performing any action.
Method 1: Using the Rust FS::Metadata Function
We have access to the Metadata function from the Rust standard library from the fs module. This function allows us to provide a path to a given file. The function then returns the metadata information about the file if it exists and it returns an error if the file does not exist.
We then use this method to verify whether a given file exists.
The following example demonstrates how we can use this function to check if a file exists in the filesystem:
fn main() {
let file_path = "path/to/file.txt";
match fs::metadata(file_path) {
Ok(_) => println!("File exists!"),
Err(_) => println!("File does not exist!"),
}
}
For example, let us check the existence of the “.bashrc” file within the filesystem:
fn main() {
let file_path = ".bashrc";
match fs::metadata(file_path) {
Ok(_) => println!("File exists!"),
Err(_) => println!("File does not exist!"),
}
}
In the previous example, we start by defining the path to the file whose existence we want to verify. We then call the “fs::metadata” function and provide the path to the target file as an argument.
If the file exists, the “Ok” variant of the result type contains the file metadata. However, since we are not interested in the metadata file, we ignore the info using the “_”. If the file does not exist, the err variant of the result type indicates that the file does not exist.
We can then handle this error and print the appropriate result to the console.
Once we run the previous code, it should print the “File exists!” string as we can find it in the filesystem.
Method 2: Using the Exists Method
In Rust, we can also use the std::path::Path struct and the “exists” method to check if a given file exists within the filesystem.
Take the program syntax that is shown in the following:
let file_path = "path/to/file.txt";
let path = Path::new(file_path);
if path.exists() {
println!("File exists!");
} else {
println!("File does not exist!");
}
}
We can use it to check the existence of the “.bashrc” file on the system as shown in the following:
fn main() {
let file_path = ".bashrc";
let path = Path::new(file_path);
if path.exists() {
println!("File exists!");
} else {
println!("File does not exist!");
}
}
In the provided example, we start by creating a path instance using the path to the file whose existence we wish to check. We then call the “exists” method on the path instance which returns a Boolean value that indicates whether the file exists.
Conclusion
We learned two ways to check if a file exists in Rust. The first method uses the std::fs::metadata function to retrieve the metadata file and check for errors. The second method involves using the std::path::Path struct and its “exists” method to check if a file or directory exists. Both ways are simple and effective and can be used depending on the specific use case.