I60R
I60R

Reputation: 487

Should I prefer path.display() over debug {:?} formatting?

Is there any performance hit when using the first?

Is it possible that some characters will be displayed improperly when using the second?

Upvotes: 1

Views: 1034

Answers (1)

mcarton
mcarton

Reputation: 30061

As explained in the documentation, Path::display is for safely printing paths that may contain non-Unicode data.

Debug preserves those characters, but is not meant to be presented to the end-user. Also, Debug surrounds the path with quotes.

For example on Linux:

use std::path::Path;
use std::os::unix::ffi::OsStrExt;
use std::ffi::OsStr;

fn main() {
    let path = OsStr::from_bytes(b"./foo/bar\xff.txt");
    let path = Path::new(path);

    println!("{}", path.display()); // ./foo/bar�.txt
    println!("{:?}", path); // "./foo/bar\xFF.txt"
}

(Permalink to the playground)

Upvotes: 7

Related Questions