2022-12-21 16:36:30 +01:00
# `HashMap`
Standard hash map with protection against HashDoS attacks:
```rust,editable
use std::collections::HashMap;
fn main() {
let mut page_counts = HashMap::new();
page_counts.insert("Adventures of Huckleberry Finn".to_string(), 207);
page_counts.insert("Grimms' Fairy Tales".to_string(), 751);
page_counts.insert("Pride and Prejudice".to_string(), 303);
if !page_counts.contains_key("Les Misérables") {
2023-02-10 15:53:27 +01:00
println!("We know about {} books, but not Les Misérables.",
2022-12-21 16:36:30 +01:00
page_counts.len());
}
for book in ["Pride and Prejudice", "Alice's Adventure in Wonderland"] {
match page_counts.get(book) {
Some(count) => println!("{book}: {count} pages"),
None => println!("{book} is unknown.")
}
}
2023-02-09 21:45:43 +00:00
// Use the .entry() method to insert a value if nothing is found.
for book in ["Pride and Prejudice", "Alice's Adventure in Wonderland"] {
let page_count: & mut i32 = page_counts.entry(book.to_string()).or_insert(0);
*page_count += 1;
}
println!("{page_counts:#?}");
2022-12-21 16:36:30 +01:00
}
```
2023-02-09 21:45:43 +00:00
< details >
2023-02-13 12:42:35 -08:00
* `HashMap` is not defined in the prelude and needs to be brought into scope.
* Try the following lines of code. The first line will see if a book is in the hashmap and if not return an alternative value. The second line will insert the alternative value in the hashmap if the book is not found.
```rust,ignore
let pc1 = page_counts
.get("Harry Potter and the Sorcerer's Stone ")
.unwrap_or(&336);
let pc2 = page_counts
.entry("The Hunger Games".to_string())
.or_insert(374);
```
2023-02-09 21:45:43 +00:00
* Unlike `vec!` , there is unfortunately no standard `hashmap!` macro.
< / details >