rename a directory in rust

To rename a directory in Rust, we first need to use the std::fs module, which provides various utilities for working with the filesystem. In this case, we'll use the std::fs::rename function, which allows us to rename a directory:

main.rs
use std::fs;

fn main() -> std::io::Result<()> {
    // Specify the old directory name
    let old_dir_name = "old_directory_name";

    // Specify the new directory name
    let new_dir_name = "new_directory_name";

    // Rename the directory
    fs::rename(old_dir_name, new_dir_name)?;

    Ok(())
}
304 chars
15 lines

In this example, we first specify the old directory name and the new directory name. We then call the fs::rename function, passing in the old directory name and the new directory name as arguments. Finally, we use the ? operator to propagate any errors that may occur.

Note that if the directory you're trying to rename doesn't exist, or if there's already a directory with the new name, an error will be returned.

gistlibby LogSnag