4 releases (2 breaking)

new 0.3.0 Nov 5, 2024
0.2.0 Jan 20, 2024
0.1.1 Jan 13, 2024
0.1.0 Jan 8, 2024

#1 in #mut

Download history 1/week @ 2024-07-23 2/week @ 2024-09-24

170 downloads per month

MIT/Apache

17KB
288 lines

MutStr

Contributors Forks Stargazers Issues

MutStr is a mutable alternative for &str.

  • &str
  • MutStr
    • uses 16 bytes.
  • String
    • uses 24 bytes.

MutStr was written as a replacement for Box<str> and String for hashtables. If you don't change the data at runtime, use Box<str>. If you prefer speed when adding new data, choose String. If you need low memory consumption and changeable data, choose MutStr.

[!TIP] MutStr is compatible with serde.

Use features as in the following example to be able to use serde:
mutstr = { version = "0.3.0", features = ["serde"] }

Examples

Assign

You can easily add new values or remove existing ones with MutStr.

use mutstr::mutstr;

fn main() {
  let mut result = mutstr::from("hello");
  result += " my friend"; // Add -> " my friend"
  result -= " friend"; // Remove -> " friend"
  assert_eq!(result.as_str(), "hello my");
  
  result.push(" friend friend friend"); // Add -> " friend friend friend"
  result -= (2, " friend"); // Remove(2 times) -> " friend"
  assert_eq!(result.as_str(), "hello my friend");
  
  result += String::from(" :)"); // Add -> " :)"
  assert_eq!(result.as_str(), "hello my friend :)");
}

MutStr as &str

Get a &str from a MutStr.

use mutstr::mutstr;

fn main() {
  let (first, second) = (
    "hello friend",
    mutstr::from("hello friend")
  );
  assert_eq!(first, second.as_str());
}

Hashtables and Vectors

You can easily use MutStr in hash tables and vectors, like you can with Box<str>.

use std::collections::HashMap;
use mutstr::mutstr;

fn main() {
  let mut result = HashMap::<Box<str>, mutstr>::new();
  result.insert(Box::from("hello"), mutstr::from("friend"));
  
  let value = result.get_mut("hello").unwrap();
  *value += " :)";
  
  assert_eq!(value.as_str(), "friend :)");
}

Dependencies

~0–295KB