Suppose I have a HashMap and want to get a mutable link to an entry, or if that entry does not exist, I want to change the link to a new object, how can I do this? I tried using unwrap_or() , something like this:
fn foo() { let mut map: HashMap<&str, Vec<&str>> = HashMap::new(); let mut ref = map.get_mut("whatever").unwrap_or( &mut Vec::<&str>::new() );
But this does not work, because Vec lifetime is not long enough. Is there any way to tell Rust that I want the returned Vec have the same lifetime as foo() ? I mean there is this obvious solution, but I feel that there should be a better way:
fn foo() { let mut map: HashMap<&str, Vec<&str>> = HashMap::new(); let mut dummy: Vec<&str> = Vec::new(); let mut ref = map.get_mut("whatever").unwrap_or( &dummy );
rust lifetime
Timmmm
source share