belongs to collection: Rust HashMap Programs
In this program, we will create a HashMap and then we will insert items into HashMap using the insert() function. After that, we will print keys and values of created HashMap using the iter() method.
Program/Source Code:
The source code to print Keys/values of a HashMap using the iter() method is given below. The given program is compiled and executed successfully.
// Rust program to print Keys/values // of a HashMap using iter() method 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 Keys/values:"); for (key, val) in map.iter() { println!("{} -> {}", key, val); } }
Output:
HashMap Keys/values: Key3 -> 103 Key1 -> 101 Key2 -> 102 Key4 -> 104
Explanation:
Here we created a HashMap. Then we inserted the item into HashMap and printed the keys/values of created HashMap using the iter() method.
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 print Keys/values of a HashMap using the iter() method is given below. The given program is compiled and executed successfully.
Output:
Explanation:
Here we created a HashMap. Then we inserted the item into HashMap and printed the keys/values of created HashMap using the iter() method.
need an explanation for this answer? contact us directly to get an explanation for this answer