Use std::fs::copy to duplicate files and std::fs::rename to move them in Rust.
Use std::fs::copy to duplicate a file and std::fs::rename to move it, handling errors with unwrap or match.
use std::fs;
fn main() -> std::io::Result<()> {
// Copy file
fs::copy("source.txt", "dest.txt")?;
// Move file (renames source to dest)
fs::rename("source.txt", "moved.txt")?;
Ok(())
}
Copying creates a duplicate of a file while keeping the original, like photocopying a document. Moving shifts a file to a new location or name, removing it from the original spot, similar to picking up a paper and placing it in a different folder. You use these functions when your program needs to manage files on the disk.