HashMap

use std::collections::HashMap;

fn main() {
    let mut h : HashMap<u32, u32> = HashMap::new();

    println!("is empty: {}", h.is_empty());

    h.insert(42, 42);

    println!("is empty: {}, h: {:?}", h.is_empty(), h);

    println!("has 42: {:?}", h.contains_key(&42));
    println!("has 1442: {:?}", h.contains_key(&1442));

    let v = match h.get(&42) {
        None => {
            eprintln!("Don't have value");
            0
        },
        Some(x) => {
            eprintln!("Have value: {}", x);
            *x
        }
    };

    println!("Value: {:?}", v);

    let v = match h.get(&1442) {
        None => {
            eprintln!("Don't have value");
            0
        },
        Some(x) => {
            eprintln!("Have value: {}", x);
            *x
        }
    };

    println!("Value: {:?}", v);

    h.remove(&42);
    println!("is empty: {}", h.is_empty());

    for i in 1..=7 {
        h.insert(i, i);
    }

    println!("Len: {}", h.len());

    for (k, v) in &h {
        println!("{} = {}", k, v);
    }


    // With entry API

    let z = h.entry(4242).or_insert(4242);
    println!("with Entry API: {:?}", z);

    fn get_value() -> u32 {
        return 4242;
    }

    let z = h.entry(42).or_insert_with(get_value);
    println!("with Entry API w/ function: {:?}", z);
}