Q:

Rust program to insert items into a HashMap

belongs to collection: Rust HashMap Programs

0

In this program, we will create a HashMap and then we will insert items into HashMap using the insert() function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to insert items into a HashMap is given below. The given program is compiled and executed successfully.

// Rust program to insert items 
// into a HashMap

use std::collections::HashMap;

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

    map.insert("Key1", 101);
    map.insert("Key2", 102);
    map.insert("Key3", 103);
    map.insert("Key4", 104);

    println!("HashMap: \n{:?}", map);
}

Output:

HashMap: 
{"Key2": 102, "Key1": 101, "Key4": 104, "Key3": 103}

Explanation:

Here we created a HashMap. Then we inserted the item into HashMap using the insert() method and printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust program to print only keys of a HashMap... >>
<< Rust program to initialize a simple HashMap...