Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove path and get the filename in rust?

Tags:

string

rust

I have a Vector that has set of file paths that gets list of mp3 files.what i need to do is remove the path and get the filename from the each of the vector item so if i have /home/user/Downloads/filename.ext the extracted string should be filename.ext how can i implement in rust?

like image 811
devMe Avatar asked Dec 31 '25 06:12

devMe


1 Answers

You can try this (std::path::Path utility in the standard library)

use std::path::Path;

let path = Path::new("my_folder/file.txt");
let filename = path.file_name().unwrap();

println!("{}", filename.to_str().unwrap());
like image 129
Deze Avatar answered Jan 04 '26 13:01

Deze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!