The source code to find the Union of two HashSets is given below. The given program is compiled and executed successfully.
// Rust program to find the
// Union of two HashSets
use std::collections::HashSet;
fn main() {
let set1: HashSet<_> = [10, 15, 30, 20,12].iter().cloned().collect();
let set2: HashSet<_> = [10, 15, 20,40].iter().cloned().collect();
println!("Union of set1 and set2:");
for item in set1.union(&set2) {
print!("{} ", item);
}
}
Output:
Union of set1 and set2:
15 12 10 30 20 40
Explanation:
Here we created two HashSets to store integer elements. Then we found the union of both sets using the union() method. After that, we printed the result.
Program/Source Code:
The source code to find the Union of two HashSets is given below. The given program is compiled and executed successfully.
Output:
Explanation:
Here we created two HashSets to store integer elements. Then we found the union of both sets using the union() method. After that, we printed the result.
need an explanation for this answer? contact us directly to get an explanation for this answer