2 unstable releases
0.2.0 | Feb 18, 2022 |
---|---|
0.1.0 | Sep 16, 2021 |
#32 in #convert-string
3KB
rust string to &'static str
how-to-convert-a-string-into-a-static-str
copy form here(how-to-convert-a-string-into-a-static-str)
cannot return reference to temporary value
usage:
use static_str::to_str;
fn main() {
let option_value = Some(123);
let v = option_value.map_or("", |v| to_str(v.to_string()));
assert_eq!(v, "123");
}
lib.rs
:
convert a string into a static str and string to &str
fix cannot return reference to temporary value
fn string_to_static_str_works() {
let option_value = Some(123);
// cannot return reference to temporary value
// let v = option_value.map_or("", |v| v.to_string().as_str());
let v = option_value.map_or("", |v| to_str(v.to_string()));
assert_eq!(v, "123");
}