std-method

Path::file_stem and extension

Inspect the basename and final extension. A compound suffix such as tar.gz is not treated as a single extension; a leading dot alone does not mark an extension. Expected stdout (not executed): backup.tar gz Common mistake: Assuming extension validates a file format.

Formal Syntax & Structure

rust syntaxrust
path.file_stem() -> Option<&OsStr>; path.extension() -> Option<&OsStr>

Working Example

Example Snippetrust
fn main() {    use std::path::Path;    let p = Path::new("backup.tar.gz");    println!("{} {}", p.file_stem().unwrap().to_str().unwrap(), p.extension().unwrap().to_str().unwrap());}
Rust Reference