Calculate the intersection of two sets
Rust Programming Language
Problem
Rust program that calculates the intersection of two sets using the HashSet data structure.
Input
use std::collections::HashSet;fn main() {let set1: HashSet<i32> = vec![1, 2, 3, 4, 5].into_iter().collect();let set2: HashSet<i32> = vec![3, 4, 5, 6, 7].into_iter().collect();let intersection = set1.intersection(&set2);let intersection_vec: Vec<&i32> = intersection.collect();println!("Intersection: {:?}", intersection_vec);}{codeBox}
Output
Intersection: [3, 4, 5]{codeBox}
Explanation
This program creates two sets, set1 and set2, using HashSet. It then calculates their intersection using the intersection method provided by HashSet. Finally, it collects the intersection into a vector, intersection_vec, and prints it out.