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.
Program/Source Code:
The source code to create HashSet from a vector is given below. The given program is compiled and executed successfully.
Output:
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