Answer#
Windows has an extended-length path form, written with the prefix \\?\. It tells the system to use the path as given, which allows paths longer than the traditional limit. Rust's fs::canonicalize returns paths in this form on Windows, and std documents that this is what it does there.
The result is correct but surprising: if you print it, pass it to another program, compare it as text with a path you got elsewhere, or show it to the user, the prefix is in the way.
use std::fs;
use std::path::Path;
fn main() -> std::io::Result<()> {
let canon = fs::canonicalize(Path::new(r"C:\Windows"))?;
println!("canonicalize: {}", canon.display());
// std::path::absolute makes a path absolute without asking the file system and without the verbatim prefix.
let abs = std::path::absolute(Path::new(r"C:\Windows\System32\..\Fonts"))?;
println!("absolute: {}", abs.display());
println!("exists: {}", abs.exists());
Ok(())
}canonicalize: \\?\C:\Windows
absolute: C:\Windows\Fonts
exists: trueWhich one to use:
- You want an absolute path to store or show:
std::path::absolute. It does not require the path to exist and does not follow symbolic links. In the run above it also resolved the... - You need the real location, with links followed, and the file must exist:
canonicalize. Expect the prefix on Windows, and strip it for display only if you are sure the path is short and has no special parts. - You compare two paths for "same file": canonicalize both (so both have the same form), or compare file identity with a crate made for it. Do not compare strings.
Common mistakes#
- Passing a
\\?\path to a tool that does not understand it. Some programs fail on it. - Assuming
canonicalizeworks for a file that does not exist yet. It returns an error; useabsolute.
Related questions#
Does this happen on macOS?#
No: the prefix is a Windows form. On macOS canonicalize returns an ordinary absolute path with links resolved (std documentation; not run on a Mac here).
Sources#
- std::fs::canonicalize: https://doc.rust-lang.org/std/fs/fn.canonicalize.html (read 2026-12-05)
- std::path::absolute: https://doc.rust-lang.org/std/path/fn.absolute.html (read 2026-12-05)
- Microsoft, Naming Files, Paths, and Namespaces: https://learn.microsoft.com/windows/win32/fileio/naming-a-file (read 2026-12-05)