PathBuf::starts_with doesn't work with backslashes

use std::path::PathBuf;

fn main() {
    let p = PathBuf::from(r"\\?\C:\test");
    println!("{}", p.starts_with(r"\\?\"));
    println!("{}", p.starts_with("r\\"));
    println!("{}", p.to_string_lossy().starts_with(r"\\?\"));



When I try the same with / instead of \ then it works as expected. Why doesn't starts_with work with backslashes?

TL;DR: Windows extended paths are messy and annoying. \\?\ is "effectively" ignored (as it (roughly, I don't really understand extended paths) just explicitly states "look for a drive name"), so the PathBuf API skips it for the path introspection API, such that \\?\C:\path behaves the same as C:\path.

//?/C:/path doesn't have this behavior, because it isn't a Windows extended path. (In fact, windows file explorer will try to treat this path as a URI and open it in your browser.)

Thanks for your answer. BTW: \\?\ just tells Windows to break the 260 characters limitation for paths. IMHO this is just a workaround for a broken design.

This behavior of PathBuf is annoying and should be explicitly mentioned in the docs.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.