home ~ projects ~ socials

Get A Filename From A Path In Rust

use std::path::PathBuf;

fn main() {

  let path = PathBuf::from("/Users/alan/Desktop/here.txt");
  println!("{}", path.file_name().unwrap().to_str().unwrap());

}

This goes through OsStr which is why there are two unwraps.

-- end of line --