Macro to create a HashMap to be a counter



examples/macros/create-counter-hash/src/main.rs
macro_rules! create_counter {
    ($( $name: expr ),*) => {
        std::collections::HashMap::from([
            $(
                ($name, 0),
            )*
        ])
    };
}

fn main() {
    let mut counter = create_counter!("cat", "dog");

    // This basically generates code like this:
    // let mut counter = std::collections::HashMap::from([
    //     ("cat", 0),
    //     ("dog", 0),
    // ]);

    println!("{:?}", counter);
    *counter.entry("dog").or_insert(0) += 1;
    println!("{:?}", counter);
}

{"dog": 0, "cat": 0}
{"dog": 1, "cat": 0}