Advertisement
cwchen

[Rust] set binary operation demo

Aug 23rd, 2017
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.74 KB | None | 0 0
  1. // Call HashSet from library
  2. use std::collections::HashSet;
  3.  
  4. fn main() {
  5.     // Get two sets from two arrays
  6.     let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
  7.     let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
  8.  
  9.     // Get the union of the two sets
  10.     let union: HashSet<_> = a.union(&b).cloned().collect();
  11.     assert_eq!(union, [1, 2, 3, 4].iter().cloned().collect());
  12.  
  13.     // Get the intersection of the two sets
  14.     let intersection: HashSet<_> = a.intersection(&b).cloned().collect();
  15.     assert_eq!(intersection, [2, 3].iter().cloned().collect());
  16.  
  17.     // Get the difference of a from b
  18.     let diff: HashSet<_> = a.difference(&b).cloned().collect();
  19.     assert_eq!(diff, [1].iter().cloned().collect());
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement