remove file rust
To remove a file in Rust, you can use the std::fs::remove_file
function. This function takes a Path
argument representing the path to the file you want to remove. Here's an example of how you can use this function:
use std::fs;
fn main() {
let path = "path/to/file.txt";
match fs::remove_file(path) {
Ok(()) => println!("File removed successfully."),
Err(e) => println!("Error removing file: {}", e),
}
}
In this example, we import the std::fs
module and use the remove_file
function to remove the file specified by the path
variable. The function returns a Result
indicating whether the file was successfully removed or if an error occurred. We use the match
statement to handle the result and print a corresponding message.
Make sure to replace "path/to/file.txt"
with the actual path to the file you want to remove.