add a key-value pair to a map in rust

To add a key-value pair to a HashMap in Rust, you can use the insert method provided by the HashMap type. Here's an example:

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

fn main() {
    // create a new HashMap
    let mut map = HashMap::new();

    // add a key-value pair
    map.insert("hello", "world");

    // print the map
    println!("{:?}", map);
}
220 chars
13 lines

This will create a new HashMap, add the key-value pair "hello": "world", and then print the resulting map:

main.rs
{"hello": "world"}
19 chars
2 lines

You can add as many key-value pairs to the map as you need using the insert method. If a key already exists in the map, its corresponding value will be replaced with the new value.

gistlibby LogSnag