HashMap Entry API
Medium
+3 pts
Collections
4/5
In Python, dictionaries handle missing keys gracefully:
counts = {} counts['a'] = counts.get('a', 0) + 1 # increment or initialize # Or with defaultdict from collections import defaultdict counts = defaultdict(int) counts['a'] += 1
Rust's HashMap has an entry API that's even more powerful.
Basic HashMap operations
use std::collections::HashMap; let mut map: HashMap<String, i32> = HashMap::new(); map.insert("key".to_string(), 42); let value = map.get("key"); // Option<&i32> let value = map["key"]; // panics if missing
The Entry API
…Login to see the full exercise.
Topics