Advertisement
cwchen

[Rust] set demo

Aug 23rd, 2017
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.67 KB | None | 0 0
  1. // Call HashSet from library
  2. use std::collections::HashSet;
  3.  
  4. fn main() {
  5.     // Create an empty set
  6.     let mut set = HashSet::new();
  7.  
  8.     // Insert item into the set
  9.     set.insert("C++");
  10.     set.insert("Java");
  11.     set.insert("Python");
  12.     set.insert("Rust");
  13.     set.insert("Go");
  14.  
  15.     // Check the property of the set
  16.     assert_eq!(set.len(), 5);
  17.     assert_eq!(set.contains("Rust"), true);
  18.     assert_eq!(set.contains("Lisp"), false);
  19.  
  20.     // Remove item from the set
  21.     set.remove("Python");
  22.  
  23.     // Check the property of the set
  24.     assert_eq!(set.len(), 4);
  25.     assert_eq!(set.contains("Rust"), true);
  26.     assert_eq!(set.contains("Python"), false);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement