Crates.io | pathtrim |
lib.rs | pathtrim |
version | 2.0.0 |
source | src |
created_at | 2021-12-09 05:08:28.132163 |
updated_at | 2023-06-24 04:29:30.739045 |
description | This crate implements the TrimmablePath trait on anything that implements AsRef |
homepage | |
repository | https://github.com/rrrodzilla/pathtrim |
max_upload_size | |
id | 494978 |
size | 15,594 |
pathtrim
is a simple, yet powerful Rust library that helps you obtain the last n parts of a filesystem path. It provides a seamless way to work with paths in a cross-platform manner, making it a great choice for projects that need to run on different systems like Unix, macOS, and Windows.
With pathtrim
, you get a clean and intuitive API for handling path trimming, out-of-the-box support for different platforms, and the confidence that comes with strong safety guarantees, thanks to the use of Rust's features and ecosystem.
Add this to your Cargo.toml
:
[dependencies]
pathtrim = "0.2.0"
And then import the trait in the file where you want to use it:
use std::path::Path;
use pathtrim::TrimmablePath;
use std::path::Path;
use pathtrim::TrimmablePath;
let path = Path::new("/usr/local/bin/application");
let trimmed = path.trim_to_nth(2).unwrap();
assert_eq!(trimmed.to_str().unwrap(), "bin/application");
Unix:
use std::path::Path;
use pathtrim::TrimmablePath;
let path = Path::new("/usr/local/bin/application");
let trimmed = path.trim_to_nth(3).unwrap();
assert_eq!(trimmed.to_str().unwrap(), "local/bin/application");
Windows:
use std::path::Path;
use pathtrim::TrimmablePath;
let path = Path::new(r"C:\Program Files\package\bin\application");
let trimmed = path.trim_to_nth(2).unwrap();
assert_eq!(trimmed.to_str().unwrap(), r"bin\application");
use std::path::Path;
use pathtrim::TrimmablePath;
let path = Path::new("/");
let trimmed = path.trim_to_nth(1);
assert!(trimmed.is_none());
To gain a deeper understanding of path components and various related APIs, refer to the official Rust documentation:
This project is licensed under the MIT License.