belongs to collection: Rust HashMap Programs
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.
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Program/Source Code:
The source code to initialize a simple HashMap is given below. The given program is compiled and executed successfully.
Output:
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