Q:

Rust program to find the length of HashMap

0

In this program, we will create a HashMap and then we will insert items into HashMap using the insert() function. After that, we will find the length of HashMap using the len() method and print HashMap.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to find the length of HashMap is given below. The given program is compiled and executed successfully.

// Rust program to find the length of 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!("Length of HashMap: {}\n",map.len());
    
    println!("HashMap Keys/values:");
    for (key, val) in map.iter() {
        println!("{} -> {}", key, val);
    }
}

Output:

Length of HashMap: 4

HashMap Keys/values:
Key2 -> 102
Key3 -> 103
Key1 -> 101
Key4 -> 104

Explanation:

Here, we created a HashMap. Then we inserted the item into HashMap and find the length of HashMap using the len() method and printed the HashMap.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now