Get A Filename From A Rust Path Without The Extension
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.gz
the output will bealfa.tar
instead ofalfa
-
There's a
file_prefix()
in nightly Rust that gets the name up until the first dot. (i.e. givenalfa.tar.gz
it will returnalfa
) -
file_stem()
returns anOsString
. I'm converting it to a regularString
here withto_string_lossy()
. It makes sure that invalid UTF characters are replaced with a placeholder instead of breaking the string
-- end of line --