mut-str
A toolkit for working with mutable string slices (&mut str
), and immutable ones too!
Pretty much all you can do safely in the standard library with mutable string slices is make them lower or upper case. This package allows for splitting and slicing by character index (rather than byte index), replacing strings and using references to characters.
All functions on string slices are available either at the package root or as methods on the StrExt
trait.
sh
cargo add mut-str
``` rust use mut_str::StrExt;
let mut welcome = Box::
// Split by character index let (l, r) = welcome.charsplitatmut(7).unwrap(); asserteq!(l, "Hello, "); assert_eq!(r, "World!");
// Replace string slices l.replacewith("mut-str").unwrap(); asserteq!(l, "mut-str");
// Replace with padding let sub = r.replacewithpadleftchar("👑!", ' ').unwrap(); asserteq!(sub, "👑!"); asserteq!(r, " 👑!");
assert_eq!(&*welcome, "mut-str 👑!");
// Get character references let crown = welcome.getcharmut(8).unwrap(); assert_eq!(crown, &'👑');
// Mutate characters crown.replace('🌍').unwrap(); assert_eq!(crown, &'🌍');
// Slice by character index let l = welcome.charslicemut(..7).unwrap(); l.replacewithpadleftspace("👋").unwrap();
assert_eq!(&*welcome, " 👋 🌍!"); ```
mut-str
on crates.io
mut-str
on GitHub
mut-str
is dual-licensed under either the Apache License Version 2.0 or MIT license at your option.