How to do it...

  1. In the folder src/bin, create a file called hashset.rs.
  2. Add the following code, and run it with cargo run --bin hashset:
1   use std::collections::HashSet;
2
3 fn main() {
4 // Most of the interface of HashSet
5 // is the same as HashMap, just without
6 // the methods that handle values
7 let mut books = HashSet::new();
8 books.insert("Harry Potter and the Philosopher's Stone");
9 books.insert("The Name of the Wind");
10 books.insert("A Game of Thrones");
11
12 // A HashSet will ignore duplicate entries
13 // but will return if an entry is new or not
14 let is_new = books.insert("The Lies of Locke Lamora");
15 if is_new {
16 println!("We've just added a new book!");
17 }
18
19 let is_new = books.insert("A Game of Thrones");
20 if !is_new {
21 println!("Sorry, we already had that book in store");
22 }
23
24 // Check if it contains a key
25 if !books.contains("The Doors of Stone") {
26 println!("We sadly don't have that book yet");
27 }
28
29 // Remove an entry
30 let was_removed = books.remove("The Darkness that comes
before");
31 if !was_removed {
32 println!("Couldn't remove book; We didn't have it to begin
with");
33 }
34 let was_removed = books.remove("Harry Potter and the
Philosopher's Stone");
35 if was_removed {
36 println!("Oops, we lost a book");
37 }
  1. Compare different HashSets:
41    let one_to_five: HashSet<_> = (1..6).collect();
42 let five_to_ten: HashSet<_> = (5..11).collect();
43 let one_to_ten: HashSet<_> = (1..11).collect();
44 let three_to_eight: HashSet<_> = (3..9).collect();
45
46 // Check if two HashSets have no elements in common
47 let is_disjoint = one_to_five.is_disjoint(&five_to_ten);
48 println!(
49 "is {:?} disjoint from {:?}?: {}",
50 one_to_five,
51 five_to_ten,
52 is_disjoint
53 );
54 let is_disjoint = one_to_five.is_disjoint(&three_to_eight);
55 println!(
56 "is {:?} disjoint from {:?}?: {}",
57 one_to_five,
58 three_to_eight,
59 is_disjoint
60 );
61
62 // Check if a HashSet is fully contained in another
63 let is_subset = one_to_five.is_subset(&five_to_ten);
64 println!(
65 "is {:?} a subset of {:?}?: {}",
66 one_to_five,
67 five_to_ten,
68 is_subset
69 );
70 let is_subset = one_to_five.is_subset(&one_to_ten);
71 println!(
72 "is {:?} a subset of {:?}?: {}",
73 one_to_five,
74 one_to_ten,
75 is_subset
76 );
77
78 // Check if a HashSet fully contains another
79 let is_superset = three_to_eight.is_superset(&five_to_ten);
80 println!(
81 "is {:?} a superset of {:?}?: {}",
82 three_to_eight,
83 five_to_ten,
84 is_superset
85 );
86 let is_superset = one_to_ten.is_superset(&five_to_ten);
87 println!(
88 "is {:?} a superset of {:?}?: {}",
89 one_to_ten,
90 five_to_ten,
91 is_superset
92 );
  1. Join two HashSets in various ways:
96    // Get the values that are in the first HashSet
97 // but not in the second
98 let difference = one_to_five.difference(&three_to_eight);
99 println!(
100 "The difference between {:?} and {:?} is {:?}",
101 one_to_five,
102 three_to_eight,
103 difference
104 );
105
106 // Get the values that are in either HashSets, but not in both
107 let symmetric_difference =
one_to_five.symmetric_difference(&three_to_eight);
108 println!(
109 "The symmetric difference between {:?} and {:?} is {:?}",
110 one_to_five,
111 three_to_eight,
112 symmetric_difference
113 );
114
115 // Get the values that are in both HashSets
116 let intersection = one_to_five.intersection(&three_to_eight);
117 println!(
118 "The intersection difference between {:?} and {:?} is {:?}",
119 one_to_five,
120 three_to_eight,
121 intersection
122 );
123
124 // Get all values in both HashSets
125 let union = one_to_five.union(&three_to_eight);
126 println!(
127 "The union difference between {:?} and {:?} is {:?}",
128 one_to_five,
129 three_to_eight,
130 union
131 );
132 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.93