Q:

Rust program to create HashSet from the vector

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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now