Crates.io | mutstr |
lib.rs | mutstr |
version | 0.4.0 |
source | src |
created_at | 2024-01-08 09:37:40.504583 |
updated_at | 2024-11-14 00:31:04.449566 |
description | A mutable alternative for `&str` |
homepage | |
repository | https://github.com/ThisAccountHasBeenSuspended/MutStr |
max_upload_size | |
id | 1092318 |
size | 32,257 |
MutStr
is a mutable alternative for &str
.
&str
MutStr
String
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 withserde
.Use
features
as in the following example to be able to useserde
:
mutstr = { version = "0.4.0", features = ["serde"] }
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());
}
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 :)");
}