ADVERTISEMENT
ADVERTISEMENT

Rust program to check the given HashMap is empty or not

Rust | HashMap Example: Write a program to check whether the given HashMap is empty or not.
Submitted by Nidhi, on October 17, 2021

Problem Solution:

In this program, we will create two HashMaps and then we will check created maps are empty or not using the is_empty() method.

Program/Source Code:

The source code to check given HashMap is empty or not is given below. The given program is compiled and executed successfully.

// Rust program to check the given 
// HashMap is empty or not

use std::collections::HashMap;

fn main() 
{
    let mut map1 = HashMap::new();
    let mut map2: HashMap<&str, i32> = [].iter().cloned().collect();
    
    map1.insert("Key1", 101);
    map1.insert("Key2", 102);
    map1.insert("Key3", 103);
    map1.insert("Key4", 104);

    if map1.is_empty()
    {
        println!("The map1 is an empty HashMap");
    }
    else
    {
        println!("The map1 is not an empty HashMap");
    }
    
    if map2.is_empty()
    {
        println!("The map2 is an empty HashMap");
    }
    else
    {
        println!("The map2 is not an empty HashMap");
    }
}

Output:

The map1 is not an empty HashMap
The map2 is an empty HashMap

Explanation:

Here, we created two HashMaps. Then we checked created HashMaps are empty or not using the is_empty() method and printed the appropriate message.

Rust HashMap Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.