ADVERTISEMENT
ADVERTISEMENT

Rust program to delete all items from HashSet

Rust | HashSet Example: Write a program to delete all items from HashSet.
Submitted by Nidhi, on October 26, 2021

Problem Solution:

In this program, we will create a HashSet to store integer items, and then we will insert items using the insert() method and delete all items from HashSet using the clear() method.

Program/Source Code:

The source code to delete all items from HashSet is given below. The given program is compiled and executed successfully.

// Rust program to delete all items 
// from HashSet

use std::collections::HashSet;

fn main() {
    let mut set:HashSet<i32> = HashSet::new();
    
    set.insert(10);
    set.insert(20);
    
    println!("HashSet items: \n{:?}",set);
    
    set.clear();
    
    println!("HashSet items: \n{:?}",set);
}

Output:

HashSet items: 
{10, 20}
HashSet items: 
{}

Explanation:

Here, we created a HashSet to store integer elements. Then we added items using the insert() method. After that, we removed all items from HashSet using the clear() method and printed update HashSet.

Rust HashSet 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.