Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. use std::{collections::HashMap, hash::Hash};
  2.  
  3. fn get_default<'m, K, V>(map: &'m mut HashMap<K, V>, key: K) -> &'m mut V
  4. where
  5. K: Eq + Hash,
  6. V: Default
  7. {
  8. match map.get_mut(&key) {
  9. Some(value) => value,
  10. None => {
  11. map.insert(key, V::default());
  12. map.get_mut(&key).unwrap()
  13. }
  14. }
  15. }
  16.  
  17. fn main() {
  18. println!("Hello, world!");
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement