Crate mut_str

Source
Expand description

ยง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.

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 version of mut-str on crates.io
This version of mut-str on crates.io
mut-str on GitHub

ยงFeatures

Features on docs.rs

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

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

ยงLicense

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

Modulesยง

errors
Errors.
iter
Iterators over UTF-8 character references.

Structsยง

Char
A UTF-8 encoded character.
OwnedChar
An owned Char.

Traitsยง

StrExt
The StrExt trait adds some methods to string types, many of which operate on character indexes and with character references.

Functionsยง

char_slice
Slice a str in units of UTF-8 characters.
char_slice_mut
Slice a mutable str in units of UTF-8 characters.
char_split_at
Split a str in units of UTF-8 characters.
char_split_at_mut
Split a mutable str in units of UTF-8 characters.
copy_to
Copy the str to a byte buffer and get the new str containing the inserted character. Returns None if buffer is shorter than the string slice.
get_char
Get a character reference from a str and an index.
get_char_mut
Get a mutable character reference from a mutable str and an index.
replace
Replace the str with another of the same length.
replace_with_pad
Replace the str with another of the same length or shorter. The remaining bytes will be filled with pad.
replace_with_pad_char
Replace the str with another of the same length or shorter. The remaining bytes will be filled with pad, which must be one byte long.
replace_with_pad_left
Replace the str with another of the same length or shorter, right aligned. The remaining bytes before the character str will be filled with pad.
replace_with_pad_left_char
Replace the str with another of the same length or shorter, right aligned. The remaining bytes before the str will be filled with char, which must be one byte long.
replace_with_pad_left_space
Replace the str with another of the same length or shorter, right aligned. The remaining bytes before the str will be filled with spaces.
replace_with_pad_space
Replace the str with another of the same length or shorter. The remaining bytes will be filled with spaces.