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 only values of created HashMap.
The source code to print only values of a HashMap is given below. The given program is compiled and executed successfully.
// Rust program to print only values
// of a HashMap
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 values:");
for val in map.values() {
println!(" {}", val);
}
}
Output:
HashMap values:
102
104
101
103
Explanation:
Here, we created a HashMap, then we inserted the item into HashMap and printed the only values of created HashMap using the values() method.
Program/Source Code:
The source code to print only values of a HashMap 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 only values of created HashMap using the values() method.
need an explanation for this answer? contact us directly to get an explanation for this answer