- HashMap
- new
- insert
- len
- mut
Create empty HashMap, insert key-value pairs
- When we create a HashMap we don't necessarily have to define the types of the keys and the values as those can be deducted from the later assignments.
- If we'd like to add new key-value pairs to the hash, we need to declare it as mutable.
- The insert method allows us to add a new key-value pair.
- len will tell us the number of keys.
examples/hashes/create-empty-hash/src/main.rs
use std::collections::HashMap; fn main() { let mut counter = HashMap::new(); println!("{}", counter.len()); println!("{:?}", counter); counter.insert("foo", 1); counter.insert("bar", 2); println!("{:?}", counter.len()); println!("{:?}", counter); }
0 {} 2 {"foo": 1, "bar": 2}