#slice #string #mutability

no-std mut-str

A toolkit for working with mutable string slices (&mut str)

8 releases (3 stable)

1.1.0-alpha.2 Jul 27, 2024
1.1.0-alpha.1 May 7, 2024
1.0.1 Oct 16, 2023
1.0.0-alpha.3 Sep 28, 2023

#227 in Text processing

Download history 130/week @ 2024-07-23 20/week @ 2024-07-30 10/week @ 2024-09-10 6/week @ 2024-09-17 8/week @ 2024-09-24 4/week @ 2024-10-01

504 downloads per month

MIT/Apache

120KB
2K SLoC

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.

cargo add mut-str

Example

use mut_str::StrExt;

let mut welcome = Box::<str>::from("Hello, World!");

// Split by character index
let (l, r) = welcome.char_split_at_mut(7).unwrap();
assert_eq!(l, "Hello, ");
assert_eq!(r, "World!");

// Replace string slices
l.replace_with("mut-str").unwrap();
assert_eq!(l, "mut-str");

// Replace with padding
let sub = r.replace_with_pad_left_char("👑!", ' ').unwrap();
assert_eq!(sub, "👑!");
assert_eq!(r, " 👑!");

assert_eq!(&*welcome, "mut-str 👑!");

// Get character references
let crown = welcome.get_char_mut(8).unwrap();
assert_eq!(crown, '👑');

// Mutate characters
crown.replace('🌍').unwrap();
assert_eq!(crown, '🌍');

// Slice by character index
let l = welcome.char_slice_mut(..7).unwrap();
l.replace_with_pad_left_space("👋").unwrap();

assert_eq!(&*welcome, "   👋 🌍!");

Latest documentation
Examples

mut-str on crates.io
mut-str on GitHub

Features

  • alloc (enabled by default) adds implementations that require the alloc library.
  • std (enabled by default, requires alloc) adds implementations specific to the standard library.
  • nightly (requires rust nightly) see below.

To make this package no-std compatible, disable the std feature.

cargo add mut-str --no-default-features
cargo add mut-str --no-default-features --features=alloc

Nightly Changes

  • Uses the extern_types feature to fix pointer provenance issues.
  • Implements Error for errors without std being enabled.

License

mut-str is dual-licensed under either the Apache License Version 2.0 or MIT license at your option.

No runtime deps

Features