A HashMap stores values under keys. Insert adds pairs and indexing reads a value back by key.

Program

Play the program to insert two scores and read one by name.

hashmap.rs
use std::collections::HashMap;

fn main() {
    let mut scores = HashMap::new();
    scores.insert("Ada", 9);
    scores.insert("Lin", 12);
    let lin = scores["Lin"];
    println!("{lin}");
}
HashMap::new `HashMap::new()` creates an empty map.
insert `insert(key, value)` stores a pair.
index `scores["Lin"]` reads the value for a known key.