std-method

Path::with_extension: derive another filename

Make an owned path with its final extension changed. It does not rename a file. Expected stdout (not executed): notes.md Common mistake: Expecting this method to perform a filesystem rename.

Formal Syntax & Structure

rust syntaxrust
path.with_extension(ext) -> PathBuf

Working Example

Example Snippetrust
fn main() {    use std::path::Path;    let p = Path::new("notes.txt");    println!("{}", p.with_extension("md").file_name().unwrap().to_str().unwrap());}
Rust Reference