我应该更喜欢 path.display() 而不是 debug {:?} 格式吗?

I60*_*60R 1 rust

使用第一个时对性能有影响吗?

使用第二个的时候会不会有些字符显示不正常?

mca*_*ton 6

如文档中所述Path::display用于安全打印可能包含非 Unicode 数据的路径。

\n\n

Debug保留这些字符,但并不意味着呈现给最终用户。另外,Debug用引号将路径引起来。

\n\n

例如在 Linux 上:

\n\n
use std::path::Path;\nuse std::os::unix::ffi::OsStrExt;\nuse std::ffi::OsStr;\n\nfn main() {\n    let path = OsStr::from_bytes(b"./foo/bar\\xff.txt");\n    let path = Path::new(path);\n\n    println!("{}", path.display()); // ./foo/bar\xef\xbf\xbd.txt\n    println!("{:?}", path); // "./foo/bar\\xFF.txt"\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

游乐场的永久链接

\n