Rust program to find the length of HashSet

Rust | HashSet Example: Write a program to find the length of HashSet.
Last Updated : October 24, 2021

Problem Statement

In this program, we will create a HashSet to store integer items, and then we will find the length of HashSet using the len() method.

Program/Source Code

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

// Rust program to find the length of HashSet

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!("Length of HashSet: {}",set.len());
    
    println!("HashSet: ");
    for item in set
    {
        print!("{} ",item);   
    }      
}

Output

Length of HashSet: 5
HashSet: 
30 50 10 20 40

Explanation

Here, we created a HashSet to store integer elements. Then we added items using the insert() method and found the length of HashSet using the len() method. After that, we iterated HashSet items using the "for" loop and printed them.

Rust HashSet Programs »



Related Programs

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.