Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit 6b5d5ba

Browse files
committed
union::bitand borrows (not taking) original HashSets
1 parent 0611002 commit 6b5d5ba

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/collections/set/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ impl<'a, T, I> Iterator for Difference<'a, T, I>
116116
}
117117
}
118118

119-
impl<T> BitAnd for HashSet<T>
119+
impl<'a, 'b, T,> BitAnd<&'b HashSet<T>> for &'a HashSet<T>
120120
where
121121
T: Hash + Eq + Clone,
122122
{
123123
type Output = HashSet<T>;
124124

125-
fn bitand(self, rhs: Self) -> HashSet<T> {
125+
fn bitand(self, rhs: &'b HashSet<T>) -> HashSet<T> {
126126
self.union(&rhs).cloned().collect()
127127
}
128128
}
@@ -213,12 +213,14 @@ mod hash_set {
213213
let mut s2: HashSet<&str> = HashSet::new();
214214
s2.insert("rat");
215215

216-
// TODO: Overload the '&' operator!
217-
let union = s1 & s2;
216+
let union = &s1 & &s2;
218217
assert!(union.contains("cat"), "union of s1 and s2 contains cat (from s1)");
219218
assert!(union.contains("dog"), "union of s1 and s2 contains dog (from s1)");
220219
assert!(union.contains("rat"), "union of s1 and s2 contains rat (from s2)");
221220
assert_eq!(union.len(), 3, "length of union is 3");
221+
222+
assert_eq!(s1.len(), 2, "s1 still available");
223+
assert_eq!(s2.len(), 1, "s2 still available");
222224
}
223225

224226

0 commit comments

Comments
 (0)