Rust program to print only keys of a HashMap

Rust | HashMap Example: Write a program to print only keys of a HashMap.
Submitted by Nidhi, on October 14, 2021

Problem Solution:

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 the keys of created HashMap.

Program/Source Code:

The source code to print only keys of a HashMap is given below. The given program is compiled and executed successfully.

// Rust program to print only keys 
// 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 Keys:");
    for key in map.keys() {
        println!("  {}", key);
    }
}

Output:

HashMap Keys:
  Key1
  Key4
  Key2
  Key3

Explanation:

Here we created a HashMap. Then we inserted the item into HashMap and printed the only keys of created HashMap using the keys() method.

Rust HashMap Programs »



Related Programs

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




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.