ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.