Get A Filename From A Rust Path Without The Extension
January 2024
TL;DR
Use file_stem() to get a filename from a path without its extensio
use PathBuf;
Output:
alfa
Notes
-
file_stem()gets the filename up until the last dot. That means if the input is something likealfa.tar.gzthe output will bealfa.tarinstead ofalfa -
There's a
file_prefix()in nightly Rust that gets the name up until the first dot. (i.e. givenalfa.tar.gzit will returnalfa) -
file_stem()returns anOsString. I'm converting it to a regularStringhere withto_string_lossy(). It makes sure that invalid UTF characters are replaced with a placeholder instead of breaking the string
end of line