How to use HashMap in Rust

Create a mutable HashMap, insert key-value pairs with insert(), and retrieve values using get() with a fallback.

Use std::collections::HashMap to store key-value pairs by creating a mutable map, inserting entries with insert, and retrieving values with get.

use std::collections::HashMap;

fn main() {
    let mut scores = HashMap::new();
    scores.insert(String::from("Blue"), 10);
    scores.insert(String::from("Yellow"), 50);

    let team_name = String::from("Blue");
    let score = scores.get(&team_name).copied().unwrap_or(0);
    println!("Score for {}: {}", team_name, score);
}