Q:

Rust program to create HashSet from the vector

belongs to collection: Rust HashSet Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust program to iterate HashSet items using the \&... >>
<< Rust program to create a simple HashSet...