Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Call HashSet from library
- use std::collections::HashSet;
- fn main() {
- // Get two sets from two arrays
- let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
- let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
- // Get the union of the two sets
- let union: HashSet<_> = a.union(&b).cloned().collect();
- assert_eq!(union, [1, 2, 3, 4].iter().cloned().collect());
- // Get the intersection of the two sets
- let intersection: HashSet<_> = a.intersection(&b).cloned().collect();
- assert_eq!(intersection, [2, 3].iter().cloned().collect());
- // Get the difference of a from b
- let diff: HashSet<_> = a.difference(&b).cloned().collect();
- assert_eq!(diff, [1].iter().cloned().collect());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement