| Crates.io | strip-prefix-suffix-sane |
| lib.rs | strip-prefix-suffix-sane |
| version | 0.1.0 |
| created_at | 2025-06-01 01:42:37.416429+00 |
| updated_at | 2025-06-01 01:42:37.416429+00 |
| description | A small utility crate providing 'sane' `strip_prefix` and `strip_suffix` methods for `str` that return `self` instead of an `Option` when the prefix/suffix is not found. |
| homepage | |
| repository | https://github.com/juzeon/strip-prefix-suffix-sane |
| max_upload_size | |
| id | 1696828 |
| size | 15,295 |
A small Rust utility crate providing "sane" strip_prefix and strip_suffix methods for str that return &self instead of an Option when the prefix/suffix is not found.
The standard library's str::strip_prefix and str::strip_suffix methods return an Option<&str>. While this is idiomatic Rust for indicating success or failure, it often leads to boilerplate code like:
let path = "/usr/local/bin";
let stripped_path = path.strip_prefix("/").unwrap_or(path);
// or
let stripped_path = if let Some(s) = path.strip_prefix("/") { s } else { path };
This crate provides a convenience trait, StripPrefixSuffixSane, that encapsulates this common unwrap_or(self) pattern, leading to more concise and readable code when you simply want the original string back if the prefix/suffix isn't present.
strip_prefix_sane(&self, prefix: &str) -> &str: Removes prefix if present; otherwise, returns self.strip_suffix_sane(&self, suffix: &str) -> &str: Removes suffix if present; otherwise, returns self.str.no_std compatible.Add this to your Cargo.toml:
[dependencies]
strip-prefix-suffix-sane = "0.1" # Use the latest version
Simply bring the StripPrefixSuffixSane trait into scope to use the new methods on &str types.
use strip-prefix-suffix-sane::StripPrefixSuffixSane;
fn main() {
let s = "hello_world";
// --- strip_prefix_sane ---
// Stripping a present prefix
assert_eq!(s.strip_prefix_sane("hello_"), "world");
// Stripping a non-present prefix returns the original string
assert_eq!(s.strip_prefix_sane("foo_"), "hello_world");
// Empty prefix returns original string
assert_eq!(s.strip_prefix_sane(""), "hello_world");
// Prefix longer than string returns original string
assert_eq!("foo".strip_prefix_sane("foobar"), "foo");
// --- strip_suffix_sane ---
// Stripping a present suffix
assert_eq!(s.strip_suffix_sane("_world"), "hello");
// Stripping a non-present suffix returns the original string
assert_eq!(s.strip_suffix_sane("_foo"), "hello_world");
// Empty suffix returns original string
assert_eq!(s.strip_suffix_sane(""), "hello_world");
// Suffix longer than string returns original string
assert_eq!("bar".strip_suffix_sane("foobar"), "bar");
// --- Chaining operations ---
let path = "/usr/local/bin/";
assert_eq!(path.strip_prefix_sane("/").strip_suffix_sane("/"), "usr/local/bin");
let filename = "image.png.bak";
assert_eq!(filename.strip_suffix_sane(".bak").strip_suffix_sane(".png"), "image");
// --- Edge cases: Empty strings ---
assert_eq!("".strip_prefix_sane("foo"), "");
assert_eq!("".strip_suffix_sane("bar"), "");
assert_eq!("".strip_prefix_sane(""), "");
assert_eq!("".strip_suffix_sane(""), "");
}