find the name of a directory in rust

To find the name of a directory in Rust, you can use the Path struct from the std::path module. Here's an example:

main.rs
use std::path::Path;

fn main() {
    let path = Path::new("/path/to/directory/");
    let directory_name = path.file_name().unwrap();
    println!("{}", directory_name.to_str().unwrap());
}
191 chars
8 lines

In this code, we create a Path object that represents the directory we want to find the name of. We then call the file_name method on the path, which returns an optional OsStr representing the file name or last component of the path.

We then use the unwrap method to extract the OsStr value and call the to_str method on it to convert it to a &str. Finally, we print the directory name to the console.

gistlibby LogSnag