Rust program to initialize a simple HashMap

Rust | HashMap Example: Write a program to initialize a simple HashMap.
Submitted by Nidhi, on October 14, 2021

Problem Solution:

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

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.

Rust HashMap Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.