Cross-platform path manipulation.
This module provides two types,
Path (akin to
str), for working with paths abstractly. These types are thin wrappers
OsStr respectively, meaning that they work directly
on strings according to the local platform’s path syntax.
Paths can be parsed into
Components by iterating over the structure
returned by the
components method on
correspond to the substrings between path separators (
\). You can
reconstruct an equivalent path from components with the
push method on
PathBuf; note that the paths may differ syntactically by the
normalization described in the documentation for the
Path manipulation includes both parsing components from slices and building new owned paths.
use std::path::Path; use std::ffi::OsStr; let path = Path::new("/tmp/foo/bar.txt"); let parent = path.parent(); assert_eq!(parent, Some(Path::new("/tmp/foo"))); let file_stem = path.file_stem(); assert_eq!(file_stem, Some(OsStr::new("bar"))); let extension = path.extension(); assert_eq!(extension, Some(OsStr::new("txt")));
To build or modify paths, use
use std::path::PathBuf; // This way works... let mut path = PathBuf::from("c:\\"); path.push("windows"); path.push("system32"); path.set_extension("dll"); // ... but push is best used if you don't know everything up // front. If you do, this way is better: let path: PathBuf = ["c:\\", "windows", "system32.dll"].iter().collect();
A structure wrapping a Windows path prefix as well as its unparsed string representation.
A single component of a path.
Windows path prefixes, e.g.,
Component parsing works by a double-ended state machine; the cursors at the front and back of the path each keep track of what parts of the path have been consumed so far.
The primary separator of path components for the current platform.
Says whether the first byte after the prefix is a separator.
Determines whether the character is one of the permitted path separators for the current platform.