Read and deserialize key-value pair JSON into HashMap



examples/json/deserialize-to-hashmap/data.json
{
    "cat": 4,
    "chicken": 2,
    "spider": 8,
    "ant": 6,
    "centipede": 100,
    "snake": 0
}

leggs:     {"ant": 6, "snake": 0, "centipede": 100, "spider": 8, "cat": 4, "chicken": 2}
turbofish: {"spider": 8, "ant": 6, "centipede": 100, "chicken": 2, "snake": 0, "cat": 4}


examples/json/deserialize-to-hashmap/src/main.rs
use std::collections::HashMap;

fn main() {
    let filename = "data.json";
    let content = std::fs::read_to_string(filename).unwrap();

    let leggs: HashMap<String, u32> = serde_json::from_str(&content).unwrap();
    println!("leggs:     {leggs:?}");

    // doing the same using Turbofish
    let turbofish = serde_json::from_str::<HashMap<String, u32>>(&content).unwrap();
    println!("turbofish: {turbofish:?}");

    assert_eq!(leggs, turbofish);
}