Q:

Rust program to iterate HashSet items using the iter() method

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 iterate HashSet items using the iter() 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 iterate HashSet items using the iter() method is given below. The given program is compiled and executed successfully.

// Rust program to iterate HashSet items 
// using iter() method

use std::collections::HashSet;

fn main() {
    let mut set:HashSet<i32> = HashSet::new();
    
    set.insert(10);
    set.insert(20);
    set.insert(30);
    set.insert(40);
    set.insert(50);
 
    println!("HashSet: ");
    for item in set.iter()
    {
        print!("{} ",item);   
    }
}

Output:

HashSet: 
30 10 20 40 50

Explanation:

Here, we created a HashSet to store integer elements. Then we added items using the insert() method. After that, we iterate HashSet elements using the iter() method and printed them.

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

total answers (1)

Rust program to delete all items from HashSet... >>
<< Rust program to check an item contains in HashSet ...