Guest User

Untitled

a guest
Jun 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. #![feature(nll)]
  2.  
  3. use std::collections::HashMap;
  4.  
  5. fn get_default(map: &mut HashMap<usize, String>, key: usize) -> &mut String {
  6. match map.get_mut(&key) {
  7. Some(value) => value,
  8. None => {
  9. map.insert(key, "".to_string());
  10. map.get_mut(&key).unwrap()
  11. }
  12. }
  13. }
  14.  
  15. fn main() {
  16. let map = &mut HashMap::new();
  17. map.insert(22, format!("Hello, world"));
  18. map.insert(44, format!("Goodbye, world"));
  19. assert_eq!(&*get_default(map, 22), "Hello, world");
  20. assert_eq!(&*get_default(map, 66), "");
  21. }
Add Comment
Please, Sign In to add comment