- unsert
- unwrap_or
- get
- copied
Accessing values
- unwrap_or(0) sets a default value to 0
examples/hashes/accessing-values/src/main.rs
use std::collections::HashMap; fn main() { let mut grades = HashMap::new(); grades.insert(String::from("Joe"), 70); grades.insert(String::from("Jane"), 80); let name = String::from("Joe"); let score = grades.get(&name); println!("{:?}", score); let score = grades[&name]; println!("{}", score); let score = grades.get(&name).copied().unwrap_or(0); println!("{}", score); let name = String::from("George"); let score = grades.get(&name).copied().unwrap_or(0); println!("{}", score); }
Some(70) 70 70 0