Struct compact_str::CompactStr [−][src]
pub struct CompactStr { /* fields omitted */ }
Expand description
A CompactStr
is a memory efficient immuatable string that can be used almost anywhere a String
or &str
can be used.
Using CompactStr
use compact_str::CompactStr;
use std::collections::HashMap;
// CompactStr auto derefs into a str so you can use all methods from `str` that take a `&self`
if CompactStr::new("hello world!").is_ascii() {
println!("we're all ASCII")
}
// You can use a CompactStr in collections like you would a String or &str
let mut map: HashMap<CompactStr, CompactStr> = HashMap::new();
// directly construct a new `CompactStr`
map.insert(CompactStr::new("nyc"), CompactStr::new("empire state building"));
// create a `CompactStr` from a `&str`
map.insert("sf".into(), "transamerica pyramid".into());
// create a `CompactStr` from a `String`
map.insert(String::from("sea").into(), String::from("space needle").into());
fn wrapped_print<T: AsRef<str>>(text: T) {
println!("{}", text.as_ref());
}
// CompactStr impls AsRef<str> and Borrow<str>, so it can be used anywhere that excepts a generic string
if let Some(building) = map.get("nyc") {
wrapped_print(building);
}
// CompactStr can also be directly compared to a String or &str
assert_eq!(CompactStr::new("chicago"), "chicago");
assert_eq!(CompactStr::new("houston"), String::from("houston"));
Implementations
Trait Implementations
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Auto Trait Implementations
impl RefUnwindSafe for CompactStr
impl Send for CompactStr
impl Sync for CompactStr
impl Unpin for CompactStr
impl UnwindSafe for CompactStr
Blanket Implementations
Mutably borrows from an owned value. Read more