pub trait TrimmablePath: AsRef<Path> {
// Provided method
fn trim_to_nth(&self, n: usize) -> Option<PathBuf> { ... }
}
Expand description
A trait to easily obtain the last n parts of a path.
This trait is implemented for anything that implements
AsRef<std::path::Path>
, so you can use it on types like
std::path::Path
and std::path::PathBuf
.
§Examples
use std::path::Path;
use pathtrim::TrimmablePath;
let p = Path::new("/usr/local/bin/");
let trimmed = p.trim_to_nth(1).unwrap();
assert_eq!(trimmed.to_str().unwrap(), "bin");
Provided Methods§
Sourcefn trim_to_nth(&self, n: usize) -> Option<PathBuf>
fn trim_to_nth(&self, n: usize) -> Option<PathBuf>
Returns a new PathBuf
containing the last n
components of the path,
or None
if there are not enough components in the path.
§Examples
use std::path::Path;
use pathtrim::TrimmablePath;
let p = Path::new("/usr/local/bin/");
let trimmed = p.trim_to_nth(2).unwrap();
assert_eq!(trimmed.to_str().unwrap(), "local/bin");