Rust program to create HashSet from the vector

Rust | HashSet Example: Write a program to create HashSet from the vector.
Submitted by Nidhi, on October 24, 2021

Problem Solution:

In this program, we will create a HashSet from vector and then we will print HashSet items.

Program/Source Code:

The source code to create HashSet from a vector is given below. The given program is compiled and executed successfully.

// Rust program to create HashSet 
// from vector

use std::collections::HashSet;

fn main() {
    let mut set:HashSet<i32> = vec![10,20,30,40,50].into_iter().collect();
    
    println!("HashSet:\n{:?}",set);
}

Output:

HashSet:
{40, 50, 20, 30, 10}

Explanation:

Here, we created a HashSet from the vector using into_iter() and collect() methods. Then we printed HashSet.

Rust HashSet Programs »



Related Programs

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




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.