7 stable releases
1.0.6 | Dec 10, 2021 |
---|---|
1.0.1 | Dec 9, 2021 |
#184 in Operating systems
25 downloads per month
6KB
pathtrim
When all you need is the last few parts of a path.
This crate implements the TrimmablePath trait on anything that implements AsRef<std::path::Path> so you can easily obtain the last n parts of a path. One good implementor of AsRef<std::path::Path> that comes to mind is std::path::Path
Usage
use std::path::Path;
// at the top of your source file
use pathtrim::TrimmablePath;
// TrimmablePath is automatically implemented for all
// std::path::Paths in the module's scope
let path = Path::new("/usr/local/bin/");
let trimmed = path.trim_to_nth(1).unwrap();
assert_eq!(trimmed.to_str().unwrap(), "bin");
let trimmed = path.trim_to_nth(2).unwrap();
assert_eq!(trimmed.to_str().unwrap(), "local/bin");
let trimmed = path.trim_to_nth(3).unwrap();
assert_eq!(trimmed.to_str().unwrap(), "usr/local/bin");
let trimmed = path.trim_to_nth(4);
assert!(trimmed.is_none());
let trimmed = path.trim_to_nth(300);
assert!(trimmed.is_none());
let trimmed = path.trim_to_nth(2000);
assert!(trimmed.is_none());
note: patch version bumps since 1.0.0 are just documentation updates