Use std::fs::remove_file for files and std::fs::remove_dir_all for directories to delete items in Rust.
Use std::fs::remove_file for files and std::fs::remove_dir_all for directories in Rust.
use std::fs;
fn main() {
// Delete a single file
fs::remove_file("path/to/file.txt").expect("Failed to delete file");
// Delete a directory and all its contents
fs::remove_dir_all("path/to/directory").expect("Failed to delete directory");
}
Rust provides built-in functions to delete files and folders from your computer. You use one command to remove a single file and another to remove a folder along with everything inside it. Think of it like using a trash can for a single document versus clearing out an entire box of files.