use std::path::Path;
/// Removes the last file extension from a filename or path.
fn remove_file_extension(path: &str) -> String {
let path = Path::new(path);
match (path.parent(), path.file_stem()) {
(_, None) => path.to_string_lossy().into_owned(),
(Some(parent), Some(stem)) if !parent.as_os_str().is_empty() => {
parent.join(stem).to_string_lossy().into_owned()
}
(_, Some(stem)) => stem.to_string_lossy().into_owned(),
}
}
fn main() {
println!("{}", remove_file_extension("document.pdf"));
println!("{}", remove_file_extension("archive.tar.gz"));
println!("{}", remove_file_extension("/tmp/file.txt"));
println!("{}", remove_file_extension("README"));
}
/*
run:
document
archive.tar
/tmp/file
README
*/