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



Comments and Discussions!

Load comments ↻





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