Skip to content

normalize

Cleans a path lexically: drops . components and folds .. into the component before it.

Nothing touches the file system, so this works for paths that do not exist, but symbolic links are not resolved (use std::fs::canonicalize for that). A .. at the start of a relative path is kept, a .. right after the root is dropped, and a path that cleans to nothing becomes ..

use helpers4::fs::normalize;

Cargo feature fs (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features fs

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["fs"] }
pub fn normalize(path: &Path) -> PathBuf
ParameterTypeDescription
path&PathThe path to clean.

PathBuf — The cleaned path.

use helpers4::fs::normalize;
use std::path::{Path, PathBuf};

assert_eq!(normalize(Path::new("a/./b/../c")), PathBuf::from("a/c"));
assert_eq!(normalize(Path::new("../a")), PathBuf::from("../a"));
assert_eq!(normalize(Path::new("a/..")), PathBuf::from("."));

src/fs/normalize.rs