Created
July 3, 2018 00:50
-
-
Save timbod7/2111d254c3990ebaf7d9511deaa7677f to your computer and use it in GitHub Desktop.
maybe create an optional reference inside a map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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