lib.rs |
Unix path manipulation.
This crate provides two types, [`PathBuf`] and [`Path`] (akin to `String`
and `str`), for working with paths abstractly. These types are thin wrappers
around `UnixString` and `UnixStr` respectively, meaning that they work
directly on strings independently from the local platform's path syntax.
Paths can be parsed into [`Component`]s by iterating over the structure
returned by the [`components`] method on [`Path`]. [`Component`]s roughly
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 [`components`] method.
## Simple usage
Path manipulation includes both parsing components from slices and building
new owned paths.
To parse a path, you can create a [`Path`] slice from a `str`
slice and start asking questions:
```
use unix_path::Path;
use unix_str::UnixStr;
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(UnixStr::new("bar")));
let extension = path.extension();
assert_eq!(extension, Some(UnixStr::new("txt")));
```
To build or modify paths, use [`PathBuf`]:
```
use unix_path::PathBuf;
// This way works...
let mut path = PathBuf::from("/");
path.push("feel");
path.push("the");
path.set_extension("force");
// ... but push is best used if you don't know everything up
// front. If you do, this way is better:
let path: PathBuf = ["/", "feel", "the.force"].iter().collect();
```
[`Component`]: enum.Component.html
[`components`]:struct.Path.html#method.components
[`PathBuf`]: struct.PathBuf.html
[`Path`]: struct.Path.html
[`push`]: struct.PathBuf.html#method.push |
83221 |