为什么我的规范化路径的前缀是 \\?\

Why does my canonicalized path get prefixed with \\?\

我正在做一个个人项目,我试图通过规范化 Rust 中的相对路径来解决这个问题。但是,每当我这样做时,新路径都会以一个奇怪的 \?\ 序列为前缀。例如,像这样简单的东西:

let p = fs::canonicalize(".").unwrap();
println!("{}", p.display());

将产生类似于以下输出的结果:

\?\C:\Users\[...]\rustprojects\projectname

这不是一个特别的问题,因为我可以通过其他方式完成我正在尝试的事情。但是,这似乎是一种奇怪的行为,特别是如果您要以某种需要准确性的方式使用路径的字符串形式。为什么这个字符序列在结果前面,我该如何避免?

The \?\ prefix tells Windows to treat the path as is, i.e. it disables the special meaning of . and .., special device names like CON are not interpreted and the path is assumed to be absolute. It also enables using paths up to 32,767 characters (UTF-16 code units), whereas otherwise the limit is 260(除非您使用的是 Windows 10,版本 1607 或更高版本,并且您的应用程序选择了更长的路径)。

因此,\?\ 前缀可确保您获得可用的路径;删除该前缀可能会产生无法使用或解析为不同文件的路径!因此,我建议您在路径中保留该前缀