When base is a UNC share root on Windows, Path.strip_prefix returns a Path with a leading separator.
I tried this code:
let path = Path::new(r"\\?\UNC\server\share\dir1\dir2");
let base = Path::new(r"\\?\UNC\server\share");
assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir1\dir2");
I expected to see this happen: the assert_eq! should pass.
Instead, this happened: the assert_eq! fails.
assertion `left == right` failed
left: "\\dir1\\dir2"
right: "dir1\\dir2"
Note: the UNC path is what canonicalize() returns for a network share.
With this in the command prompt:
net use Z: \\server\share
The following code reproduces the issue.
let path = Path::new(r"Z:\dir1\dir2").canonicalize()?;
let base = Path::new(r"Z:\").canonicalize()?;
assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir1\dir2");
Note 2: following cases works as expected.
- When
base is not a root of a network share.
let path = Path::new(r"\\?\UNC\server\share\dir1\dir2");
let base = Path::new(r"\\?\UNC\server\share\dir1");
assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir2");
- When it's not in the
\\?\UNC form.
let path = Path::new(r"\\server\share\dir1\dir2");
let base = Path::new(r"\\server\share");
assert_eq!(path.strip_prefix(base)?.to_str().unwrap(), r"dir1\dir2");
Meta
rustc --version --verbose:
rustc 1.94.1 (e408947bf 2026-03-25)
binary: rustc
commit-hash: e408947bfd200af42db322daf0fadfe7e26d3bd1
commit-date: 2026-03-25
host: x86_64-pc-windows-msvc
release: 1.94.1
LLVM version: 21.1.8
When
baseis a UNC share root on Windows,Path.strip_prefixreturns aPathwith a leading separator.I tried this code:
I expected to see this happen: the
assert_eq!should pass.Instead, this happened: the
assert_eq!fails.Note: the UNC path is what
canonicalize()returns for a network share.With this in the command prompt:
The following code reproduces the issue.
Note 2: following cases works as expected.
baseis not a root of a network share.\\?\UNCform.Meta
rustc --version --verbose: