Skip to main content

canonicalize_lexically

Function canonicalize_lexically 

Source
pub fn canonicalize_lexically(path: PathBuf) -> PathBuf
Expand description

Normalizes all non-canonical patterns of path.

This function does not try to remove parent components on Unix, but does so on Windows. See crate documentation for more details about that.

Consider using NormpathBuf (or Normpath) if validation should be type-checked and thus enforced by the compiler.

§Examples

use std::path::PathBuf;

let path1 = PathBuf::from("//foo/./bar/");
let norm1 = canonicalize_lexically(path1);
assert_eq!(&norm1, "/foo/bar");

let path2 = PathBuf::from(".//foo/../bar/");
let norm2 = canonicalize_lexically(path2);
assert_eq!(&norm2, "foo/../bar");

Windows deals with parent components differently:

use std::path::PathBuf;

let path1 = PathBuf::from(r"C:\foo/.\bar\");
let norm1 = canonicalize_lexically(path1);
assert_eq!(&norm1, r"C:\foo\bar");

let path2 = PathBuf::from(r"./\foo\../bar\..");
let norm2 = canonicalize_lexically(path2);
assert_eq!(&norm2, "");