Skip to content

Instantly share code, notes, and snippets.

@timbod7
Created July 3, 2018 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timbod7/2111d254c3990ebaf7d9511deaa7677f to your computer and use it in GitHub Desktop.
Save timbod7/2111d254c3990ebaf7d9511deaa7677f to your computer and use it in GitHub Desktop.
maybe create an optional reference inside a map
fn get_or_insert1<'a>(map: &'a mut HashMap<String,String>, key:&String) -> &'a mut String {
map.entry(key.clone()).or_insert_with(|| String::new())
}
fn get_or_insert2<'a>(map: &'a mut HashMap<String,String>, key:&String) -> &'a mut String {
match map.get_mut(key) {
Some(v) => v,
None => {
map.entry(key.clone()).or_insert(String::new())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment