I am using Rust and I want to use a global mutable HashMap
for convenience. However, while it is possible to define a global, mutable HashMap
using lazy_static
and Mutex
, it is hard for my String
variable defined in my function to have the same life time of the gloabl HashMap
.
I've tried insert a &str directly and it worked well. Is there any way to convert a String to pure value?
lazy_static! {
static ref USER_TOKEN_HASHMAP: Mutex<HashMap<&'static str, &'static str>> = {
let mut m = HashMap::new();
Mutex::new(m)
};
}
fn func() {
let mut _map = USER_TOKEN_HASHMAP.lock().unwrap();
let user_email = String::from("aaa");
let user_password = String::from("bbb");
_map.insert(user_email.as_str(), user_password.as_str());
}
Error Info:
`user_email` does not live long enough
values in a scope are dropped in the opposite order they are defined
rustc(E0597)