mutstr

Crates.iomutstr
lib.rsmutstr
version0.4.0
sourcesrc
created_at2024-01-08 09:37:40.504583
updated_at2024-11-14 00:31:04.449566
descriptionA mutable alternative for `&str`
homepage
repositoryhttps://github.com/ThisAccountHasBeenSuspended/MutStr
max_upload_size
id1092318
size32,257
Nick-I. A. (ThisAccountHasBeenSuspended)

documentation

README

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.4.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 :)");
}
Commit count: 3

cargo fmt