Q:

Rust program to initialize a simple HashMap

belongs to collection: Rust HashMap Programs

0

In this program, we will create and initialize a simple HashMap and print created HashMap. A HashMap stores elements in Key/Value pair.

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 initialize a simple HashMap is given below. The given program is compiled and executed successfully.

// Rust program to initialize 
// a simple HashMap

use std::collections::HashMap;

fn main() {
    let map: HashMap<&str, i32> = [("key1", 101),("key2", 102),("key3", 103),].iter().cloned().collect();

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

Output:

HashMap: 
{"key3": 103, "key1": 101, "key2": 102}

Explanation:

Here we created and initialized the HashMap. Then we printed the created HashMap.

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

total answers (1)

Rust program to insert items into a HashMap... >>