std-method

Path::canonicalize: resolve an existing path

Resolve an existing path to an absolute canonical path, including symbolic links. Windows may use extended-length syntax. It is not proof of future containment. Use a fresh disposable working directory exclusively controlled by this program. Expected stdout (not executed): true true Common mistake: Using canonicalize plus a prefix test as a complete security boundary.

Formal Syntax & Structure

rust syntaxrust
path.canonicalize() -> io::Result<PathBuf>

Working Example

Example Snippetrust
fn main() {    use std::fs::{self, File};    use std::io::{Read, Write};    fs::create_dir("bm-fixture").expect("fresh working directory required");    let p = std::path::Path::new("bm-fixture").join("note.txt");    File::create_new(&p).unwrap();    let q = p.canonicalize().unwrap();    println!("{} {}", q.is_absolute(), q.is_file());}
Rust Reference