ADVERTISEMENT
ADVERTISEMENT

Rust program to create a HashSet with specified capacity

Rust | HashSet Example: Write a program to create a HashSet with specified capacity.
Submitted by Nidhi, on October 13, 2021

Problem Solution:

In this program, we will create a HashSet with specified capacity using the with_capacity() method and print the elements of created HashSet.

Program/Source Code:

The source code to create a HashSet with specified capacity is given below. The given program is compiled and executed successfully.

// Rust program to create a HashSet 
// with specified capacity

use std::collections::HashSet;

fn main() {
    let mut set = HashSet::with_capacity(3);
    
    set.insert(10);
    set.insert(20);
    
    println!("HashSet: {:?}", set);
    println!("Capacity of HashSet: {}",set.capacity());
}

Output:

HashSet: {10, 20}
Capacity of HashSet: 3

Explanation:

Here, we created a HashSet with capacity 3 using the with_capacity() method. Then we inserted items into HashSet. After that, we printed the HashSet and its capacity.

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.