ADVERTISEMENT
ADVERTISEMENT

Rust program to get the capacity of HashSet

Rust | HashSet Example: Write a program to get the capacity of HashSet.
Submitted by Nidhi, on October 13, 2021

Problem Solution:

In this program, we will create a HashSet to store integer items, and then we will get the capacity of HashSet using the capacity() method. The capacity of HashSet is always the multiple of 7. If we initialize elements more than 7 then and less than 14 it becomes 14.

Program/Source Code:

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

// Rust program to get the capacity of HashSet

use std::collections::HashSet;
fn main() {
    let mut set: HashSet<_> = [10, 20, 30,40,50].iter().cloned().collect();
    
    println!("HashSet: {:?}", set);
    println!("Capacity of HashSet: {}",set.capacity());
}

Output:

HashSet: {30, 20, 40, 50, 10}
Capacity of HashSet: 7

Explanation:

Here we created a HashSet to store integer elements. Then we got the capacity of HashSet using the capacity() function and printed the result.

Rust HashSet Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


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.