Q:

Rust program to delete all items from HashSet

belongs to collection: Rust HashSet Programs

0

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.

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

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

total answers (1)

Rust program to find the difference between two Ha... >>
<< Rust program to iterate HashSet items using the it...