Skip to content

ThisAccountHasBeenSuspended/MutStr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 :)");
}

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages