Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::{collections::HashMap, hash::Hash};
- fn get_default<'m, K, V>(map: &'m mut HashMap<K, V>, key: K) -> &'m mut V
- where
- K: Eq + Hash,
- V: Default
- {
- match map.get_mut(&key) {
- Some(value) => value,
- None => {
- map.insert(key, V::default());
- map.get_mut(&key).unwrap()
- }
- }
- }
- fn main() {
- println!("Hello, world!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement