]> git.proxmox.com Git - rustc.git/blame - library/std/src/collections/hash/set.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / collections / hash / set.rs
CommitLineData
1b1a35ee
XL
1#[cfg(test)]
2mod tests;
3
4use hashbrown::hash_set as base;
5
532ac7d7 6use crate::borrow::Borrow;
e1599b0c 7use crate::collections::TryReserveError;
532ac7d7 8use crate::fmt;
dfeec247 9use crate::hash::{BuildHasher, Hash};
532ac7d7 10use crate::iter::{Chain, FromIterator, FusedIterator};
dfeec247 11use crate::ops::{BitAnd, BitOr, BitXor, Sub};
1a4d82fc 12
1b1a35ee 13use super::map::{map_try_reserve_error, RandomState};
1a4d82fc
JJ
14
15// Future Optimization (FIXME!)
532ac7d7 16// ============================
1a4d82fc
JJ
17//
18// Iteration over zero sized values is a noop. There is no need
19// for `bucket.val` in the case of HashSet. I suppose we would need HKT
20// to get rid of it properly.
21
cc61c64b 22/// A hash set implemented as a `HashMap` where the value is `()`.
d9579d0f 23///
cc61c64b
XL
24/// As with the [`HashMap`] type, a `HashSet` requires that the elements
25/// implement the [`Eq`] and [`Hash`] traits. This can frequently be achieved by
d9579d0f
AL
26/// using `#[derive(PartialEq, Eq, Hash)]`. If you implement these yourself,
27/// it is important that the following property holds:
c34b1796
AL
28///
29/// ```text
30/// k1 == k2 -> hash(k1) == hash(k2)
31/// ```
32///
33/// In other words, if two keys are equal, their hashes must be equal.
1a4d82fc 34///
c34b1796
AL
35///
36/// It is a logic error for an item to be modified in such a way that the
cc61c64b
XL
37/// item's hash, as determined by the [`Hash`] trait, or its equality, as
38/// determined by the [`Eq`] trait, changes while it is in the set. This is
39/// normally only possible through [`Cell`], [`RefCell`], global state, I/O, or
c34b1796
AL
40/// unsafe code.
41///
42/// # Examples
1a4d82fc
JJ
43///
44/// ```
45/// use std::collections::HashSet;
46/// // Type inference lets us omit an explicit type signature (which
94b46f34 47/// // would be `HashSet<String>` in this example).
1a4d82fc
JJ
48/// let mut books = HashSet::new();
49///
50/// // Add some books.
94b46f34
XL
51/// books.insert("A Dance With Dragons".to_string());
52/// books.insert("To Kill a Mockingbird".to_string());
53/// books.insert("The Odyssey".to_string());
54/// books.insert("The Great Gatsby".to_string());
1a4d82fc
JJ
55///
56/// // Check for a specific one.
d9579d0f 57/// if !books.contains("The Winds of Winter") {
1a4d82fc
JJ
58/// println!("We have {} books, but The Winds of Winter ain't one.",
59/// books.len());
60/// }
61///
62/// // Remove a book.
d9579d0f 63/// books.remove("The Odyssey");
1a4d82fc
JJ
64///
65/// // Iterate over everything.
d9579d0f
AL
66/// for book in &books {
67/// println!("{}", book);
1a4d82fc
JJ
68/// }
69/// ```
70///
71/// The easiest way to use `HashSet` with a custom type is to derive
cc61c64b
XL
72/// [`Eq`] and [`Hash`]. We must also derive [`PartialEq`], this will in the
73/// future be implied by [`Eq`].
1a4d82fc
JJ
74///
75/// ```
76/// use std::collections::HashSet;
85aaf69f 77/// #[derive(Hash, Eq, PartialEq, Debug)]
94b46f34
XL
78/// struct Viking {
79/// name: String,
85aaf69f 80/// power: usize,
1a4d82fc
JJ
81/// }
82///
83/// let mut vikings = HashSet::new();
84///
94b46f34
XL
85/// vikings.insert(Viking { name: "Einar".to_string(), power: 9 });
86/// vikings.insert(Viking { name: "Einar".to_string(), power: 9 });
87/// vikings.insert(Viking { name: "Olaf".to_string(), power: 4 });
88/// vikings.insert(Viking { name: "Harald".to_string(), power: 8 });
1a4d82fc
JJ
89///
90/// // Use derived implementation to print the vikings.
d9579d0f 91/// for x in &vikings {
1a4d82fc
JJ
92/// println!("{:?}", x);
93/// }
94/// ```
c30ab7b3 95///
cc61c64b 96/// A `HashSet` with fixed list of elements can be initialized from an array:
c30ab7b3
SL
97///
98/// ```
99/// use std::collections::HashSet;
100///
e74abb32
XL
101/// let viking_names: HashSet<&'static str> =
102/// [ "Einar", "Olaf", "Harald" ].iter().cloned().collect();
103/// // use the values stored in the set
c30ab7b3 104/// ```
cc61c64b 105///
1b1a35ee 106/// [`HashMap`]: crate::collections::HashMap
3dfed10e
XL
107/// [`RefCell`]: crate::cell::RefCell
108/// [`Cell`]: crate::cell::Cell
1a4d82fc 109#[derive(Clone)]
f9f354fc 110#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_type")]
85aaf69f 111#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 112pub struct HashSet<T, S = RandomState> {
1b1a35ee 113 base: base::HashSet<T, S>,
1a4d82fc
JJ
114}
115
74b04a01 116impl<T> HashSet<T, RandomState> {
3b2f2976 117 /// Creates an empty `HashSet`.
1a4d82fc 118 ///
ea8adc8c
XL
119 /// The hash set is initially created with a capacity of 0, so it will not allocate until it
120 /// is first inserted into.
121 ///
c34b1796 122 /// # Examples
1a4d82fc
JJ
123 ///
124 /// ```
125 /// use std::collections::HashSet;
3b2f2976 126 /// let set: HashSet<i32> = HashSet::new();
1a4d82fc
JJ
127 /// ```
128 #[inline]
85aaf69f 129 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 130 pub fn new() -> HashSet<T, RandomState> {
1b1a35ee 131 Default::default()
1a4d82fc
JJ
132 }
133
c30ab7b3
SL
134 /// Creates an empty `HashSet` with the specified capacity.
135 ///
136 /// The hash set will be able to hold at least `capacity` elements without
137 /// reallocating. If `capacity` is 0, the hash set will not allocate.
1a4d82fc 138 ///
c34b1796 139 /// # Examples
1a4d82fc
JJ
140 ///
141 /// ```
142 /// use std::collections::HashSet;
3b2f2976
XL
143 /// let set: HashSet<i32> = HashSet::with_capacity(10);
144 /// assert!(set.capacity() >= 10);
1a4d82fc
JJ
145 /// ```
146 #[inline]
85aaf69f
SL
147 #[stable(feature = "rust1", since = "1.0.0")]
148 pub fn with_capacity(capacity: usize) -> HashSet<T, RandomState> {
1b1a35ee 149 HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, Default::default()) }
1a4d82fc
JJ
150 }
151}
152
9fa01778
XL
153impl<T, S> HashSet<T, S> {
154 /// Returns the number of elements the set can hold without reallocating.
155 ///
156 /// # Examples
157 ///
158 /// ```
159 /// use std::collections::HashSet;
160 /// let set: HashSet<i32> = HashSet::with_capacity(100);
161 /// assert!(set.capacity() >= 100);
162 /// ```
163 #[inline]
164 #[stable(feature = "rust1", since = "1.0.0")]
165 pub fn capacity(&self) -> usize {
1b1a35ee 166 self.base.capacity()
9fa01778
XL
167 }
168
169 /// An iterator visiting all elements in arbitrary order.
170 /// The iterator element type is `&'a T`.
171 ///
172 /// # Examples
173 ///
174 /// ```
175 /// use std::collections::HashSet;
176 /// let mut set = HashSet::new();
177 /// set.insert("a");
178 /// set.insert("b");
179 ///
180 /// // Will print in an arbitrary order.
181 /// for x in set.iter() {
182 /// println!("{}", x);
183 /// }
184 /// ```
48663c56 185 #[inline]
9fa01778 186 #[stable(feature = "rust1", since = "1.0.0")]
532ac7d7 187 pub fn iter(&self) -> Iter<'_, T> {
1b1a35ee 188 Iter { base: self.base.iter() }
9fa01778
XL
189 }
190
191 /// Returns the number of elements in the set.
192 ///
193 /// # Examples
194 ///
195 /// ```
196 /// use std::collections::HashSet;
197 ///
198 /// let mut v = HashSet::new();
199 /// assert_eq!(v.len(), 0);
200 /// v.insert(1);
201 /// assert_eq!(v.len(), 1);
202 /// ```
48663c56 203 #[inline]
9fa01778
XL
204 #[stable(feature = "rust1", since = "1.0.0")]
205 pub fn len(&self) -> usize {
1b1a35ee 206 self.base.len()
9fa01778
XL
207 }
208
209 /// Returns `true` if the set contains no elements.
210 ///
211 /// # Examples
212 ///
213 /// ```
214 /// use std::collections::HashSet;
215 ///
216 /// let mut v = HashSet::new();
217 /// assert!(v.is_empty());
218 /// v.insert(1);
219 /// assert!(!v.is_empty());
220 /// ```
48663c56 221 #[inline]
9fa01778
XL
222 #[stable(feature = "rust1", since = "1.0.0")]
223 pub fn is_empty(&self) -> bool {
1b1a35ee 224 self.base.is_empty()
9fa01778
XL
225 }
226
227 /// Clears the set, returning all elements in an iterator.
228 ///
229 /// # Examples
230 ///
231 /// ```
232 /// use std::collections::HashSet;
233 ///
234 /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
235 /// assert!(!set.is_empty());
236 ///
237 /// // print 1, 2, 3 in an arbitrary order
238 /// for i in set.drain() {
239 /// println!("{}", i);
240 /// }
241 ///
242 /// assert!(set.is_empty());
243 /// ```
244 #[inline]
245 #[stable(feature = "drain", since = "1.6.0")]
532ac7d7 246 pub fn drain(&mut self) -> Drain<'_, T> {
1b1a35ee
XL
247 Drain { base: self.base.drain() }
248 }
249
250 /// Creates an iterator which uses a closure to determine if a value should be removed.
251 ///
252 /// If the closure returns true, then the value is removed and yielded.
253 /// If the closure returns false, the value will remain in the list and will not be yielded
254 /// by the iterator.
255 ///
256 /// If the iterator is only partially consumed or not consumed at all, each of the remaining
257 /// values will still be subjected to the closure and removed and dropped if it returns true.
258 ///
259 /// It is unspecified how many more values will be subjected to the closure
260 /// if a panic occurs in the closure, or if a panic occurs while dropping a value, or if the
261 /// `DrainFilter` itself is leaked.
262 ///
263 /// # Examples
264 ///
265 /// Splitting a set into even and odd values, reusing the original set:
266 ///
267 /// ```
268 /// #![feature(hash_drain_filter)]
269 /// use std::collections::HashSet;
270 ///
271 /// let mut set: HashSet<i32> = (0..8).collect();
272 /// let drained: HashSet<i32> = set.drain_filter(|v| v % 2 == 0).collect();
273 ///
274 /// let mut evens = drained.into_iter().collect::<Vec<_>>();
275 /// let mut odds = set.into_iter().collect::<Vec<_>>();
276 /// evens.sort();
277 /// odds.sort();
278 ///
279 /// assert_eq!(evens, vec![0, 2, 4, 6]);
280 /// assert_eq!(odds, vec![1, 3, 5, 7]);
281 /// ```
282 #[inline]
283 #[unstable(feature = "hash_drain_filter", issue = "59618")]
284 pub fn drain_filter<F>(&mut self, pred: F) -> DrainFilter<'_, T, F>
285 where
286 F: FnMut(&T) -> bool,
287 {
288 DrainFilter { base: self.base.drain_filter(pred) }
9fa01778
XL
289 }
290
291 /// Clears the set, removing all values.
292 ///
293 /// # Examples
294 ///
295 /// ```
296 /// use std::collections::HashSet;
297 ///
298 /// let mut v = HashSet::new();
299 /// v.insert(1);
300 /// v.clear();
301 /// assert!(v.is_empty());
302 /// ```
48663c56 303 #[inline]
9fa01778
XL
304 #[stable(feature = "rust1", since = "1.0.0")]
305 pub fn clear(&mut self) {
1b1a35ee 306 self.base.clear()
9fa01778 307 }
9fa01778 308
1a4d82fc
JJ
309 /// Creates a new empty hash set which will use the given hasher to hash
310 /// keys.
311 ///
312 /// The hash set is also created with the default initial capacity.
313 ///
9cc50fc6
SL
314 /// Warning: `hasher` is normally randomly generated, and
315 /// is designed to allow `HashSet`s to be resistant to attacks that
316 /// cause many collisions and very poor performance. Setting it
317 /// manually using this function can expose a DoS attack vector.
318 ///
f9f354fc
XL
319 /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
320 /// the HashMap to be useful, see its documentation for details.
321 ///
c34b1796 322 /// # Examples
1a4d82fc
JJ
323 ///
324 /// ```
325 /// use std::collections::HashSet;
326 /// use std::collections::hash_map::RandomState;
327 ///
328 /// let s = RandomState::new();
9cc50fc6 329 /// let mut set = HashSet::with_hasher(s);
85aaf69f 330 /// set.insert(2);
1a4d82fc
JJ
331 /// ```
332 #[inline]
9cc50fc6
SL
333 #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
334 pub fn with_hasher(hasher: S) -> HashSet<T, S> {
1b1a35ee 335 HashSet { base: base::HashSet::with_hasher(hasher) }
1a4d82fc
JJ
336 }
337
0731742a 338 /// Creates an empty `HashSet` with the specified capacity, using
c30ab7b3
SL
339 /// `hasher` to hash the keys.
340 ///
341 /// The hash set will be able to hold at least `capacity` elements without
342 /// reallocating. If `capacity` is 0, the hash set will not allocate.
1a4d82fc
JJ
343 ///
344 /// Warning: `hasher` is normally randomly generated, and
345 /// is designed to allow `HashSet`s to be resistant to attacks that
346 /// cause many collisions and very poor performance. Setting it
347 /// manually using this function can expose a DoS attack vector.
348 ///
f9f354fc
XL
349 /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
350 /// the HashMap to be useful, see its documentation for details.
351 ///
c34b1796 352 /// # Examples
1a4d82fc
JJ
353 ///
354 /// ```
355 /// use std::collections::HashSet;
356 /// use std::collections::hash_map::RandomState;
357 ///
358 /// let s = RandomState::new();
9cc50fc6 359 /// let mut set = HashSet::with_capacity_and_hasher(10, s);
85aaf69f 360 /// set.insert(1);
1a4d82fc
JJ
361 /// ```
362 #[inline]
9cc50fc6 363 #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
c30ab7b3 364 pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S> {
1b1a35ee 365 HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, hasher) }
9cc50fc6
SL
366 }
367
cc61c64b
XL
368 /// Returns a reference to the set's [`BuildHasher`].
369 ///
3b2f2976
XL
370 /// # Examples
371 ///
372 /// ```
373 /// use std::collections::HashSet;
374 /// use std::collections::hash_map::RandomState;
375 ///
376 /// let hasher = RandomState::new();
377 /// let set: HashSet<i32> = HashSet::with_hasher(hasher);
378 /// let hasher: &RandomState = set.hasher();
379 /// ```
48663c56 380 #[inline]
54a0048b 381 #[stable(feature = "hashmap_public_hasher", since = "1.9.0")]
7453a54e 382 pub fn hasher(&self) -> &S {
1b1a35ee 383 self.base.hasher()
7453a54e 384 }
74b04a01 385}
7453a54e 386
74b04a01
XL
387impl<T, S> HashSet<T, S>
388where
389 T: Eq + Hash,
390 S: BuildHasher,
391{
1a4d82fc
JJ
392 /// Reserves capacity for at least `additional` more elements to be inserted
393 /// in the `HashSet`. The collection may reserve more space to avoid
394 /// frequent reallocations.
395 ///
396 /// # Panics
397 ///
85aaf69f 398 /// Panics if the new allocation size overflows `usize`.
1a4d82fc 399 ///
c34b1796 400 /// # Examples
1a4d82fc
JJ
401 ///
402 /// ```
403 /// use std::collections::HashSet;
85aaf69f 404 /// let mut set: HashSet<i32> = HashSet::new();
1a4d82fc 405 /// set.reserve(10);
3b2f2976 406 /// assert!(set.capacity() >= 10);
1a4d82fc 407 /// ```
48663c56 408 #[inline]
85aaf69f
SL
409 #[stable(feature = "rust1", since = "1.0.0")]
410 pub fn reserve(&mut self, additional: usize) {
1b1a35ee 411 self.base.reserve(additional)
1a4d82fc
JJ
412 }
413
48663c56
XL
414 /// Tries to reserve capacity for at least `additional` more elements to be inserted
415 /// in the given `HashSet<K,V>`. The collection may reserve more space to avoid
416 /// frequent reallocations.
417 ///
418 /// # Errors
419 ///
420 /// If the capacity overflows, or the allocator reports a failure, then an error
421 /// is returned.
422 ///
423 /// # Examples
424 ///
425 /// ```
426 /// #![feature(try_reserve)]
427 /// use std::collections::HashSet;
428 /// let mut set: HashSet<i32> = HashSet::new();
429 /// set.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?");
430 /// ```
431 #[inline]
dfeec247 432 #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
e1599b0c 433 pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
1b1a35ee 434 self.base.try_reserve(additional).map_err(map_try_reserve_error)
48663c56
XL
435 }
436
1a4d82fc
JJ
437 /// Shrinks the capacity of the set as much as possible. It will drop
438 /// down as much as possible while maintaining the internal rules
439 /// and possibly leaving some space in accordance with the resize policy.
440 ///
c34b1796 441 /// # Examples
1a4d82fc
JJ
442 ///
443 /// ```
444 /// use std::collections::HashSet;
445 ///
85aaf69f 446 /// let mut set = HashSet::with_capacity(100);
1a4d82fc
JJ
447 /// set.insert(1);
448 /// set.insert(2);
449 /// assert!(set.capacity() >= 100);
450 /// set.shrink_to_fit();
451 /// assert!(set.capacity() >= 2);
452 /// ```
48663c56 453 #[inline]
85aaf69f 454 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 455 pub fn shrink_to_fit(&mut self) {
1b1a35ee 456 self.base.shrink_to_fit()
1a4d82fc
JJ
457 }
458
0531ce1d
XL
459 /// Shrinks the capacity of the set with a lower limit. It will drop
460 /// down no lower than the supplied limit while maintaining the internal rules
461 /// and possibly leaving some space in accordance with the resize policy.
462 ///
463 /// Panics if the current capacity is smaller than the supplied
464 /// minimum capacity.
465 ///
466 /// # Examples
467 ///
468 /// ```
469 /// #![feature(shrink_to)]
470 /// use std::collections::HashSet;
471 ///
472 /// let mut set = HashSet::with_capacity(100);
473 /// set.insert(1);
474 /// set.insert(2);
475 /// assert!(set.capacity() >= 100);
476 /// set.shrink_to(10);
477 /// assert!(set.capacity() >= 10);
478 /// set.shrink_to(0);
479 /// assert!(set.capacity() >= 2);
480 /// ```
481 #[inline]
dfeec247 482 #[unstable(feature = "shrink_to", reason = "new API", issue = "56431")]
0531ce1d 483 pub fn shrink_to(&mut self, min_capacity: usize) {
1b1a35ee 484 self.base.shrink_to(min_capacity)
0531ce1d
XL
485 }
486
cc61c64b 487 /// Visits the values representing the difference,
0731742a 488 /// i.e., the values that are in `self` but not in `other`.
1a4d82fc 489 ///
c34b1796 490 /// # Examples
1a4d82fc
JJ
491 ///
492 /// ```
493 /// use std::collections::HashSet;
85aaf69f
SL
494 /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
495 /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
1a4d82fc
JJ
496 ///
497 /// // Can be seen as `a - b`.
498 /// for x in a.difference(&b) {
499 /// println!("{}", x); // Print 1
500 /// }
501 ///
3b2f2976
XL
502 /// let diff: HashSet<_> = a.difference(&b).collect();
503 /// assert_eq!(diff, [1].iter().collect());
1a4d82fc
JJ
504 ///
505 /// // Note that difference is not symmetric,
506 /// // and `b - a` means something else:
3b2f2976
XL
507 /// let diff: HashSet<_> = b.difference(&a).collect();
508 /// assert_eq!(diff, [4].iter().collect());
1a4d82fc 509 /// ```
48663c56 510 #[inline]
85aaf69f 511 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 512 pub fn difference<'a>(&'a self, other: &'a HashSet<T, S>) -> Difference<'a, T, S> {
dfeec247 513 Difference { iter: self.iter(), other }
1a4d82fc
JJ
514 }
515
cc61c64b 516 /// Visits the values representing the symmetric difference,
0731742a 517 /// i.e., the values that are in `self` or in `other` but not in both.
1a4d82fc 518 ///
c34b1796 519 /// # Examples
1a4d82fc
JJ
520 ///
521 /// ```
522 /// use std::collections::HashSet;
85aaf69f
SL
523 /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
524 /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
1a4d82fc
JJ
525 ///
526 /// // Print 1, 4 in arbitrary order.
527 /// for x in a.symmetric_difference(&b) {
528 /// println!("{}", x);
529 /// }
530 ///
3b2f2976
XL
531 /// let diff1: HashSet<_> = a.symmetric_difference(&b).collect();
532 /// let diff2: HashSet<_> = b.symmetric_difference(&a).collect();
1a4d82fc
JJ
533 ///
534 /// assert_eq!(diff1, diff2);
3b2f2976 535 /// assert_eq!(diff1, [1, 4].iter().collect());
1a4d82fc 536 /// ```
48663c56 537 #[inline]
85aaf69f 538 #[stable(feature = "rust1", since = "1.0.0")]
dfeec247
XL
539 pub fn symmetric_difference<'a>(
540 &'a self,
541 other: &'a HashSet<T, S>,
542 ) -> SymmetricDifference<'a, T, S> {
1a4d82fc
JJ
543 SymmetricDifference { iter: self.difference(other).chain(other.difference(self)) }
544 }
545
cc61c64b 546 /// Visits the values representing the intersection,
0731742a 547 /// i.e., the values that are both in `self` and `other`.
1a4d82fc 548 ///
c34b1796 549 /// # Examples
1a4d82fc
JJ
550 ///
551 /// ```
552 /// use std::collections::HashSet;
85aaf69f
SL
553 /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
554 /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
1a4d82fc
JJ
555 ///
556 /// // Print 2, 3 in arbitrary order.
557 /// for x in a.intersection(&b) {
558 /// println!("{}", x);
559 /// }
560 ///
3b2f2976
XL
561 /// let intersection: HashSet<_> = a.intersection(&b).collect();
562 /// assert_eq!(intersection, [2, 3].iter().collect());
1a4d82fc 563 /// ```
48663c56 564 #[inline]
85aaf69f 565 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 566 pub fn intersection<'a>(&'a self, other: &'a HashSet<T, S>) -> Intersection<'a, T, S> {
0731742a 567 if self.len() <= other.len() {
dfeec247 568 Intersection { iter: self.iter(), other }
0731742a 569 } else {
dfeec247 570 Intersection { iter: other.iter(), other: self }
1a4d82fc
JJ
571 }
572 }
573
cc61c64b 574 /// Visits the values representing the union,
0731742a 575 /// i.e., all the values in `self` or `other`, without duplicates.
1a4d82fc 576 ///
c34b1796 577 /// # Examples
1a4d82fc
JJ
578 ///
579 /// ```
580 /// use std::collections::HashSet;
85aaf69f
SL
581 /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
582 /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
1a4d82fc
JJ
583 ///
584 /// // Print 1, 2, 3, 4 in arbitrary order.
585 /// for x in a.union(&b) {
586 /// println!("{}", x);
587 /// }
588 ///
3b2f2976
XL
589 /// let union: HashSet<_> = a.union(&b).collect();
590 /// assert_eq!(union, [1, 2, 3, 4].iter().collect());
1a4d82fc 591 /// ```
48663c56 592 #[inline]
85aaf69f 593 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 594 pub fn union<'a>(&'a self, other: &'a HashSet<T, S>) -> Union<'a, T, S> {
60c5eb7d 595 if self.len() >= other.len() {
dfeec247 596 Union { iter: self.iter().chain(other.difference(self)) }
0731742a 597 } else {
dfeec247 598 Union { iter: other.iter().chain(self.difference(other)) }
0731742a 599 }
1a4d82fc
JJ
600 }
601
1a4d82fc
JJ
602 /// Returns `true` if the set contains a value.
603 ///
604 /// The value may be any borrowed form of the set's value type, but
cc61c64b 605 /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
1a4d82fc
JJ
606 /// the value type.
607 ///
c34b1796 608 /// # Examples
1a4d82fc
JJ
609 ///
610 /// ```
611 /// use std::collections::HashSet;
612 ///
85aaf69f 613 /// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
1a4d82fc
JJ
614 /// assert_eq!(set.contains(&1), true);
615 /// assert_eq!(set.contains(&4), false);
616 /// ```
48663c56 617 #[inline]
85aaf69f 618 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 619 pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool
dfeec247
XL
620 where
621 T: Borrow<Q>,
622 Q: Hash + Eq,
1a4d82fc 623 {
1b1a35ee 624 self.base.contains(value)
1a4d82fc
JJ
625 }
626
e9174d1e
SL
627 /// Returns a reference to the value in the set, if any, that is equal to the given value.
628 ///
629 /// The value may be any borrowed form of the set's value type, but
cc61c64b 630 /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
e9174d1e 631 /// the value type.
cc61c64b 632 ///
2c00a5a8
XL
633 /// # Examples
634 ///
635 /// ```
636 /// use std::collections::HashSet;
637 ///
638 /// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
639 /// assert_eq!(set.get(&2), Some(&2));
640 /// assert_eq!(set.get(&4), None);
641 /// ```
48663c56 642 #[inline]
54a0048b 643 #[stable(feature = "set_recovery", since = "1.9.0")]
e9174d1e 644 pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
dfeec247
XL
645 where
646 T: Borrow<Q>,
647 Q: Hash + Eq,
e9174d1e 648 {
1b1a35ee 649 self.base.get(value)
48663c56
XL
650 }
651
652 /// Inserts the given `value` into the set if it is not present, then
653 /// returns a reference to the value in the set.
654 ///
655 /// # Examples
656 ///
657 /// ```
658 /// #![feature(hash_set_entry)]
659 ///
660 /// use std::collections::HashSet;
661 ///
662 /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
663 /// assert_eq!(set.len(), 3);
664 /// assert_eq!(set.get_or_insert(2), &2);
665 /// assert_eq!(set.get_or_insert(100), &100);
666 /// assert_eq!(set.len(), 4); // 100 was inserted
667 /// ```
668 #[inline]
669 #[unstable(feature = "hash_set_entry", issue = "60896")]
670 pub fn get_or_insert(&mut self, value: T) -> &T {
671 // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
672 // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
1b1a35ee 673 self.base.get_or_insert(value)
48663c56
XL
674 }
675
dfeec247
XL
676 /// Inserts an owned copy of the given `value` into the set if it is not
677 /// present, then returns a reference to the value in the set.
678 ///
679 /// # Examples
680 ///
681 /// ```
682 /// #![feature(hash_set_entry)]
683 ///
684 /// use std::collections::HashSet;
685 ///
686 /// let mut set: HashSet<String> = ["cat", "dog", "horse"]
687 /// .iter().map(|&pet| pet.to_owned()).collect();
688 ///
689 /// assert_eq!(set.len(), 3);
690 /// for &pet in &["cat", "dog", "fish"] {
691 /// let value = set.get_or_insert_owned(pet);
692 /// assert_eq!(value, pet);
693 /// }
694 /// assert_eq!(set.len(), 4); // a new "fish" was inserted
695 /// ```
696 #[inline]
697 #[unstable(feature = "hash_set_entry", issue = "60896")]
698 pub fn get_or_insert_owned<Q: ?Sized>(&mut self, value: &Q) -> &T
699 where
700 T: Borrow<Q>,
701 Q: Hash + Eq + ToOwned<Owned = T>,
702 {
703 // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
704 // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
1b1a35ee 705 self.base.get_or_insert_owned(value)
dfeec247
XL
706 }
707
48663c56
XL
708 /// Inserts a value computed from `f` into the set if the given `value` is
709 /// not present, then returns a reference to the value in the set.
710 ///
711 /// # Examples
712 ///
713 /// ```
714 /// #![feature(hash_set_entry)]
715 ///
716 /// use std::collections::HashSet;
717 ///
718 /// let mut set: HashSet<String> = ["cat", "dog", "horse"]
719 /// .iter().map(|&pet| pet.to_owned()).collect();
720 ///
721 /// assert_eq!(set.len(), 3);
722 /// for &pet in &["cat", "dog", "fish"] {
723 /// let value = set.get_or_insert_with(pet, str::to_owned);
724 /// assert_eq!(value, pet);
725 /// }
726 /// assert_eq!(set.len(), 4); // a new "fish" was inserted
727 /// ```
728 #[inline]
729 #[unstable(feature = "hash_set_entry", issue = "60896")]
730 pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T
dfeec247
XL
731 where
732 T: Borrow<Q>,
733 Q: Hash + Eq,
734 F: FnOnce(&Q) -> T,
48663c56
XL
735 {
736 // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with
737 // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`.
1b1a35ee 738 self.base.get_or_insert_with(value, f)
e9174d1e
SL
739 }
740
8bb4bdeb 741 /// Returns `true` if `self` has no elements in common with `other`.
1a4d82fc
JJ
742 /// This is equivalent to checking for an empty intersection.
743 ///
c34b1796 744 /// # Examples
1a4d82fc
JJ
745 ///
746 /// ```
747 /// use std::collections::HashSet;
748 ///
85aaf69f
SL
749 /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect();
750 /// let mut b = HashSet::new();
1a4d82fc
JJ
751 ///
752 /// assert_eq!(a.is_disjoint(&b), true);
753 /// b.insert(4);
754 /// assert_eq!(a.is_disjoint(&b), true);
755 /// b.insert(1);
756 /// assert_eq!(a.is_disjoint(&b), false);
757 /// ```
85aaf69f 758 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 759 pub fn is_disjoint(&self, other: &HashSet<T, S>) -> bool {
0731742a
XL
760 if self.len() <= other.len() {
761 self.iter().all(|v| !other.contains(v))
762 } else {
763 other.iter().all(|v| !self.contains(v))
764 }
1a4d82fc
JJ
765 }
766
8bb4bdeb 767 /// Returns `true` if the set is a subset of another,
0731742a 768 /// i.e., `other` contains at least all the values in `self`.
1a4d82fc 769 ///
c34b1796 770 /// # Examples
1a4d82fc
JJ
771 ///
772 /// ```
773 /// use std::collections::HashSet;
774 ///
85aaf69f
SL
775 /// let sup: HashSet<_> = [1, 2, 3].iter().cloned().collect();
776 /// let mut set = HashSet::new();
1a4d82fc
JJ
777 ///
778 /// assert_eq!(set.is_subset(&sup), true);
779 /// set.insert(2);
780 /// assert_eq!(set.is_subset(&sup), true);
781 /// set.insert(4);
782 /// assert_eq!(set.is_subset(&sup), false);
783 /// ```
85aaf69f 784 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 785 pub fn is_subset(&self, other: &HashSet<T, S>) -> bool {
dfeec247 786 if self.len() <= other.len() { self.iter().all(|v| other.contains(v)) } else { false }
1a4d82fc
JJ
787 }
788
8bb4bdeb 789 /// Returns `true` if the set is a superset of another,
0731742a 790 /// i.e., `self` contains at least all the values in `other`.
1a4d82fc 791 ///
c34b1796 792 /// # Examples
1a4d82fc
JJ
793 ///
794 /// ```
795 /// use std::collections::HashSet;
796 ///
85aaf69f
SL
797 /// let sub: HashSet<_> = [1, 2].iter().cloned().collect();
798 /// let mut set = HashSet::new();
1a4d82fc
JJ
799 ///
800 /// assert_eq!(set.is_superset(&sub), false);
801 ///
802 /// set.insert(0);
803 /// set.insert(1);
804 /// assert_eq!(set.is_superset(&sub), false);
805 ///
806 /// set.insert(2);
807 /// assert_eq!(set.is_superset(&sub), true);
808 /// ```
809 #[inline]
85aaf69f 810 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
811 pub fn is_superset(&self, other: &HashSet<T, S>) -> bool {
812 other.is_subset(self)
813 }
814
b039eaaf
SL
815 /// Adds a value to the set.
816 ///
a7813a04 817 /// If the set did not have this value present, `true` is returned.
b039eaaf 818 ///
a7813a04 819 /// If the set did have this value present, `false` is returned.
1a4d82fc 820 ///
c34b1796 821 /// # Examples
1a4d82fc
JJ
822 ///
823 /// ```
824 /// use std::collections::HashSet;
825 ///
826 /// let mut set = HashSet::new();
827 ///
85aaf69f 828 /// assert_eq!(set.insert(2), true);
1a4d82fc
JJ
829 /// assert_eq!(set.insert(2), false);
830 /// assert_eq!(set.len(), 1);
831 /// ```
48663c56 832 #[inline]
85aaf69f 833 #[stable(feature = "rust1", since = "1.0.0")]
c30ab7b3 834 pub fn insert(&mut self, value: T) -> bool {
1b1a35ee 835 self.base.insert(value)
c30ab7b3 836 }
1a4d82fc 837
e9174d1e
SL
838 /// Adds a value to the set, replacing the existing value, if any, that is equal to the given
839 /// one. Returns the replaced value.
2c00a5a8
XL
840 ///
841 /// # Examples
842 ///
843 /// ```
844 /// use std::collections::HashSet;
845 ///
846 /// let mut set = HashSet::new();
847 /// set.insert(Vec::<i32>::new());
848 ///
849 /// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
850 /// set.replace(Vec::with_capacity(10));
851 /// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);
852 /// ```
48663c56 853 #[inline]
54a0048b 854 #[stable(feature = "set_recovery", since = "1.9.0")]
e9174d1e 855 pub fn replace(&mut self, value: T) -> Option<T> {
1b1a35ee 856 self.base.replace(value)
e9174d1e
SL
857 }
858
9fa01778 859 /// Removes a value from the set. Returns whether the value was
1a4d82fc
JJ
860 /// present in the set.
861 ///
862 /// The value may be any borrowed form of the set's value type, but
cc61c64b 863 /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
1a4d82fc
JJ
864 /// the value type.
865 ///
c34b1796 866 /// # Examples
1a4d82fc
JJ
867 ///
868 /// ```
869 /// use std::collections::HashSet;
870 ///
871 /// let mut set = HashSet::new();
872 ///
85aaf69f 873 /// set.insert(2);
1a4d82fc
JJ
874 /// assert_eq!(set.remove(&2), true);
875 /// assert_eq!(set.remove(&2), false);
876 /// ```
48663c56 877 #[inline]
85aaf69f 878 #[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 879 pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
dfeec247
XL
880 where
881 T: Borrow<Q>,
882 Q: Hash + Eq,
1a4d82fc 883 {
1b1a35ee 884 self.base.remove(value)
1a4d82fc 885 }
e9174d1e
SL
886
887 /// Removes and returns the value in the set, if any, that is equal to the given one.
888 ///
889 /// The value may be any borrowed form of the set's value type, but
cc61c64b 890 /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
e9174d1e 891 /// the value type.
cc61c64b 892 ///
2c00a5a8
XL
893 /// # Examples
894 ///
895 /// ```
896 /// use std::collections::HashSet;
897 ///
898 /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
899 /// assert_eq!(set.take(&2), Some(2));
900 /// assert_eq!(set.take(&2), None);
901 /// ```
48663c56 902 #[inline]
54a0048b 903 #[stable(feature = "set_recovery", since = "1.9.0")]
e9174d1e 904 pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T>
dfeec247
XL
905 where
906 T: Borrow<Q>,
907 Q: Hash + Eq,
e9174d1e 908 {
1b1a35ee 909 self.base.take(value)
e9174d1e 910 }
8bb4bdeb
XL
911
912 /// Retains only the elements specified by the predicate.
913 ///
914 /// In other words, remove all elements `e` such that `f(&e)` returns `false`.
915 ///
916 /// # Examples
917 ///
918 /// ```
8bb4bdeb
XL
919 /// use std::collections::HashSet;
920 ///
921 /// let xs = [1,2,3,4,5,6];
0531ce1d 922 /// let mut set: HashSet<i32> = xs.iter().cloned().collect();
8bb4bdeb
XL
923 /// set.retain(|&k| k % 2 == 0);
924 /// assert_eq!(set.len(), 3);
925 /// ```
cc61c64b 926 #[stable(feature = "retain_hash_collection", since = "1.18.0")]
1b1a35ee 927 pub fn retain<F>(&mut self, f: F)
dfeec247
XL
928 where
929 F: FnMut(&T) -> bool,
8bb4bdeb 930 {
1b1a35ee 931 self.base.retain(f)
8bb4bdeb 932 }
1a4d82fc
JJ
933}
934
85aaf69f
SL
935#[stable(feature = "rust1", since = "1.0.0")]
936impl<T, S> PartialEq for HashSet<T, S>
dfeec247
XL
937where
938 T: Eq + Hash,
939 S: BuildHasher,
1a4d82fc
JJ
940{
941 fn eq(&self, other: &HashSet<T, S>) -> bool {
c30ab7b3
SL
942 if self.len() != other.len() {
943 return false;
944 }
1a4d82fc
JJ
945
946 self.iter().all(|key| other.contains(key))
947 }
948}
949
85aaf69f
SL
950#[stable(feature = "rust1", since = "1.0.0")]
951impl<T, S> Eq for HashSet<T, S>
dfeec247
XL
952where
953 T: Eq + Hash,
954 S: BuildHasher,
c30ab7b3
SL
955{
956}
1a4d82fc 957
85aaf69f
SL
958#[stable(feature = "rust1", since = "1.0.0")]
959impl<T, S> fmt::Debug for HashSet<T, S>
dfeec247 960where
74b04a01 961 T: fmt::Debug,
1a4d82fc 962{
532ac7d7 963 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62682a34 964 f.debug_set().entries(self.iter()).finish()
1a4d82fc
JJ
965 }
966}
967
85aaf69f
SL
968#[stable(feature = "rust1", since = "1.0.0")]
969impl<T, S> FromIterator<T> for HashSet<T, S>
dfeec247
XL
970where
971 T: Eq + Hash,
972 S: BuildHasher + Default,
1a4d82fc 973{
48663c56 974 #[inline]
c30ab7b3 975 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> HashSet<T, S> {
476ff2be
SL
976 let mut set = HashSet::with_hasher(Default::default());
977 set.extend(iter);
1a4d82fc
JJ
978 set
979 }
980}
981
85aaf69f
SL
982#[stable(feature = "rust1", since = "1.0.0")]
983impl<T, S> Extend<T> for HashSet<T, S>
dfeec247
XL
984where
985 T: Eq + Hash,
986 S: BuildHasher,
1a4d82fc 987{
48663c56 988 #[inline]
c30ab7b3 989 fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
1b1a35ee 990 self.base.extend(iter);
1a4d82fc 991 }
f9f354fc
XL
992
993 #[inline]
994 fn extend_one(&mut self, item: T) {
1b1a35ee 995 self.base.insert(item);
f9f354fc
XL
996 }
997
998 #[inline]
999 fn extend_reserve(&mut self, additional: usize) {
1b1a35ee 1000 self.base.extend_reserve(additional);
f9f354fc 1001 }
1a4d82fc
JJ
1002}
1003
e9174d1e
SL
1004#[stable(feature = "hash_extend_copy", since = "1.4.0")]
1005impl<'a, T, S> Extend<&'a T> for HashSet<T, S>
dfeec247
XL
1006where
1007 T: 'a + Eq + Hash + Copy,
1008 S: BuildHasher,
e9174d1e 1009{
48663c56 1010 #[inline]
c30ab7b3 1011 fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
e9174d1e
SL
1012 self.extend(iter.into_iter().cloned());
1013 }
f9f354fc
XL
1014
1015 #[inline]
1016 fn extend_one(&mut self, &item: &'a T) {
1b1a35ee 1017 self.base.insert(item);
f9f354fc
XL
1018 }
1019
1020 #[inline]
1021 fn extend_reserve(&mut self, additional: usize) {
1022 Extend::<T>::extend_reserve(self, additional)
1023 }
e9174d1e
SL
1024}
1025
85aaf69f
SL
1026#[stable(feature = "rust1", since = "1.0.0")]
1027impl<T, S> Default for HashSet<T, S>
dfeec247 1028where
74b04a01 1029 S: Default,
1a4d82fc 1030{
9e0c209e 1031 /// Creates an empty `HashSet<T, S>` with the `Default` value for the hasher.
48663c56 1032 #[inline]
1a4d82fc 1033 fn default() -> HashSet<T, S> {
1b1a35ee 1034 HashSet { base: Default::default() }
1a4d82fc
JJ
1035 }
1036}
1037
85aaf69f 1038#[stable(feature = "rust1", since = "1.0.0")]
532ac7d7 1039impl<T, S> BitOr<&HashSet<T, S>> for &HashSet<T, S>
dfeec247
XL
1040where
1041 T: Eq + Hash + Clone,
1042 S: BuildHasher + Default,
1a4d82fc
JJ
1043{
1044 type Output = HashSet<T, S>;
1045
1046 /// Returns the union of `self` and `rhs` as a new `HashSet<T, S>`.
1047 ///
1048 /// # Examples
1049 ///
1050 /// ```
1051 /// use std::collections::HashSet;
1052 ///
85aaf69f
SL
1053 /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1054 /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
1a4d82fc 1055 ///
85aaf69f 1056 /// let set = &a | &b;
1a4d82fc
JJ
1057 ///
1058 /// let mut i = 0;
1059 /// let expected = [1, 2, 3, 4, 5];
62682a34 1060 /// for x in &set {
1a4d82fc
JJ
1061 /// assert!(expected.contains(x));
1062 /// i += 1;
1063 /// }
1064 /// assert_eq!(i, expected.len());
1065 /// ```
1066 fn bitor(self, rhs: &HashSet<T, S>) -> HashSet<T, S> {
1067 self.union(rhs).cloned().collect()
1068 }
1069}
1070
85aaf69f 1071#[stable(feature = "rust1", since = "1.0.0")]
532ac7d7 1072impl<T, S> BitAnd<&HashSet<T, S>> for &HashSet<T, S>
dfeec247
XL
1073where
1074 T: Eq + Hash + Clone,
1075 S: BuildHasher + Default,
1a4d82fc
JJ
1076{
1077 type Output = HashSet<T, S>;
1078
1079 /// Returns the intersection of `self` and `rhs` as a new `HashSet<T, S>`.
1080 ///
1081 /// # Examples
1082 ///
1083 /// ```
1084 /// use std::collections::HashSet;
1085 ///
85aaf69f
SL
1086 /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1087 /// let b: HashSet<_> = vec![2, 3, 4].into_iter().collect();
1a4d82fc 1088 ///
85aaf69f 1089 /// let set = &a & &b;
1a4d82fc
JJ
1090 ///
1091 /// let mut i = 0;
1092 /// let expected = [2, 3];
62682a34 1093 /// for x in &set {
1a4d82fc
JJ
1094 /// assert!(expected.contains(x));
1095 /// i += 1;
1096 /// }
1097 /// assert_eq!(i, expected.len());
1098 /// ```
1099 fn bitand(self, rhs: &HashSet<T, S>) -> HashSet<T, S> {
1100 self.intersection(rhs).cloned().collect()
1101 }
1102}
1103
85aaf69f 1104#[stable(feature = "rust1", since = "1.0.0")]
532ac7d7 1105impl<T, S> BitXor<&HashSet<T, S>> for &HashSet<T, S>
dfeec247
XL
1106where
1107 T: Eq + Hash + Clone,
1108 S: BuildHasher + Default,
1a4d82fc
JJ
1109{
1110 type Output = HashSet<T, S>;
1111
1112 /// Returns the symmetric difference of `self` and `rhs` as a new `HashSet<T, S>`.
1113 ///
1114 /// # Examples
1115 ///
1116 /// ```
1117 /// use std::collections::HashSet;
1118 ///
85aaf69f
SL
1119 /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1120 /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
1a4d82fc 1121 ///
85aaf69f 1122 /// let set = &a ^ &b;
1a4d82fc
JJ
1123 ///
1124 /// let mut i = 0;
1125 /// let expected = [1, 2, 4, 5];
62682a34 1126 /// for x in &set {
1a4d82fc
JJ
1127 /// assert!(expected.contains(x));
1128 /// i += 1;
1129 /// }
1130 /// assert_eq!(i, expected.len());
1131 /// ```
1132 fn bitxor(self, rhs: &HashSet<T, S>) -> HashSet<T, S> {
1133 self.symmetric_difference(rhs).cloned().collect()
1134 }
1135}
1136
85aaf69f 1137#[stable(feature = "rust1", since = "1.0.0")]
532ac7d7 1138impl<T, S> Sub<&HashSet<T, S>> for &HashSet<T, S>
dfeec247
XL
1139where
1140 T: Eq + Hash + Clone,
1141 S: BuildHasher + Default,
1a4d82fc
JJ
1142{
1143 type Output = HashSet<T, S>;
1144
1145 /// Returns the difference of `self` and `rhs` as a new `HashSet<T, S>`.
1146 ///
1147 /// # Examples
1148 ///
1149 /// ```
1150 /// use std::collections::HashSet;
1151 ///
85aaf69f
SL
1152 /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1153 /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
1a4d82fc 1154 ///
85aaf69f 1155 /// let set = &a - &b;
1a4d82fc
JJ
1156 ///
1157 /// let mut i = 0;
1158 /// let expected = [1, 2];
62682a34 1159 /// for x in &set {
1a4d82fc
JJ
1160 /// assert!(expected.contains(x));
1161 /// i += 1;
1162 /// }
1163 /// assert_eq!(i, expected.len());
1164 /// ```
1165 fn sub(self, rhs: &HashSet<T, S>) -> HashSet<T, S> {
1166 self.difference(rhs).cloned().collect()
1167 }
1168}
1169
cc61c64b
XL
1170/// An iterator over the items of a `HashSet`.
1171///
1172/// This `struct` is created by the [`iter`] method on [`HashSet`].
1173/// See its documentation for more.
1174///
3dfed10e 1175/// [`iter`]: HashSet::iter
1b1a35ee
XL
1176///
1177/// # Examples
1178///
1179/// ```
1180/// use std::collections::HashSet;
1181///
1182/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
1183///
1184/// let mut iter = a.iter();
1185/// ```
85aaf69f 1186#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 1187pub struct Iter<'a, K: 'a> {
1b1a35ee 1188 base: base::Iter<'a, K>,
1a4d82fc
JJ
1189}
1190
cc61c64b
XL
1191/// An owning iterator over the items of a `HashSet`.
1192///
dfeec247 1193/// This `struct` is created by the [`into_iter`] method on [`HashSet`]
cc61c64b
XL
1194/// (provided by the `IntoIterator` trait). See its documentation for more.
1195///
3dfed10e 1196/// [`into_iter`]: IntoIterator::into_iter
1b1a35ee
XL
1197///
1198/// # Examples
1199///
1200/// ```
1201/// use std::collections::HashSet;
1202///
1203/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
1204///
1205/// let mut iter = a.into_iter();
1206/// ```
85aaf69f 1207#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 1208pub struct IntoIter<K> {
1b1a35ee 1209 base: base::IntoIter<K>,
1a4d82fc
JJ
1210}
1211
cc61c64b
XL
1212/// A draining iterator over the items of a `HashSet`.
1213///
1214/// This `struct` is created by the [`drain`] method on [`HashSet`].
1215/// See its documentation for more.
1216///
3dfed10e 1217/// [`drain`]: HashSet::drain
1b1a35ee
XL
1218///
1219/// # Examples
1220///
1221/// ```
1222/// use std::collections::HashSet;
1223///
1224/// let mut a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
1225///
1226/// let mut drain = a.drain();
1227/// ```
85aaf69f 1228#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 1229pub struct Drain<'a, K: 'a> {
1b1a35ee
XL
1230 base: base::Drain<'a, K>,
1231}
1232
1233/// A draining, filtering iterator over the items of a `HashSet`.
1234///
1235/// This `struct` is created by the [`drain_filter`] method on [`HashSet`].
1236///
1237/// [`drain_filter`]: HashSet::drain_filter
1238///
1239/// # Examples
1240///
1241/// ```
1242/// #![feature(hash_drain_filter)]
1243///
1244/// use std::collections::HashSet;
1245///
1246/// let mut a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
1247///
1248/// let mut drain_filtered = a.drain_filter(|v| v % 2 == 0);
1249/// ```
1250#[unstable(feature = "hash_drain_filter", issue = "59618")]
1251pub struct DrainFilter<'a, K, F>
1252where
1253 F: FnMut(&K) -> bool,
1254{
1255 base: base::DrainFilter<'a, K, F>,
1a4d82fc
JJ
1256}
1257
cc61c64b
XL
1258/// A lazy iterator producing elements in the intersection of `HashSet`s.
1259///
1260/// This `struct` is created by the [`intersection`] method on [`HashSet`].
1261/// See its documentation for more.
1262///
3dfed10e 1263/// [`intersection`]: HashSet::intersection
1b1a35ee
XL
1264///
1265/// # Examples
1266///
1267/// ```
1268/// use std::collections::HashSet;
1269///
1270/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
1271/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
1272///
1273/// let mut intersection = a.intersection(&b);
1274/// ```
85aaf69f 1275#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
1276pub struct Intersection<'a, T: 'a, S: 'a> {
1277 // iterator of the first set
1278 iter: Iter<'a, T>,
1279 // the second set
1280 other: &'a HashSet<T, S>,
1281}
1282
cc61c64b
XL
1283/// A lazy iterator producing elements in the difference of `HashSet`s.
1284///
1285/// This `struct` is created by the [`difference`] method on [`HashSet`].
1286/// See its documentation for more.
1287///
3dfed10e 1288/// [`difference`]: HashSet::difference
1b1a35ee
XL
1289///
1290/// # Examples
1291///
1292/// ```
1293/// use std::collections::HashSet;
1294///
1295/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
1296/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
1297///
1298/// let mut difference = a.difference(&b);
1299/// ```
85aaf69f 1300#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
1301pub struct Difference<'a, T: 'a, S: 'a> {
1302 // iterator of the first set
1303 iter: Iter<'a, T>,
1304 // the second set
1305 other: &'a HashSet<T, S>,
1306}
1307
cc61c64b
XL
1308/// A lazy iterator producing elements in the symmetric difference of `HashSet`s.
1309///
1310/// This `struct` is created by the [`symmetric_difference`] method on
1311/// [`HashSet`]. See its documentation for more.
1312///
3dfed10e 1313/// [`symmetric_difference`]: HashSet::symmetric_difference
1b1a35ee
XL
1314///
1315/// # Examples
1316///
1317/// ```
1318/// use std::collections::HashSet;
1319///
1320/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
1321/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
1322///
1323/// let mut intersection = a.symmetric_difference(&b);
1324/// ```
85aaf69f 1325#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 1326pub struct SymmetricDifference<'a, T: 'a, S: 'a> {
c30ab7b3 1327 iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>,
1a4d82fc
JJ
1328}
1329
cc61c64b
XL
1330/// A lazy iterator producing elements in the union of `HashSet`s.
1331///
1332/// This `struct` is created by the [`union`] method on [`HashSet`].
1333/// See its documentation for more.
1334///
3dfed10e 1335/// [`union`]: HashSet::union
1b1a35ee
XL
1336///
1337/// # Examples
1338///
1339/// ```
1340/// use std::collections::HashSet;
1341///
1342/// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect();
1343/// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect();
1344///
1345/// let mut union_iter = a.union(&b);
1346/// ```
85aaf69f 1347#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc 1348pub struct Union<'a, T: 'a, S: 'a> {
c30ab7b3 1349 iter: Chain<Iter<'a, T>, Difference<'a, T, S>>,
1a4d82fc
JJ
1350}
1351
85aaf69f 1352#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 1353impl<'a, T, S> IntoIterator for &'a HashSet<T, S> {
85aaf69f
SL
1354 type Item = &'a T;
1355 type IntoIter = Iter<'a, T>;
1356
48663c56 1357 #[inline]
85aaf69f
SL
1358 fn into_iter(self) -> Iter<'a, T> {
1359 self.iter()
1360 }
1361}
1362
1363#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 1364impl<T, S> IntoIterator for HashSet<T, S> {
85aaf69f
SL
1365 type Item = T;
1366 type IntoIter = IntoIter<T>;
1367
9346a6ac
AL
1368 /// Creates a consuming iterator, that is, one that moves each value out
1369 /// of the set in arbitrary order. The set cannot be used after calling
1370 /// this.
1371 ///
1372 /// # Examples
1373 ///
1374 /// ```
1375 /// use std::collections::HashSet;
1376 /// let mut set = HashSet::new();
1377 /// set.insert("a".to_string());
1378 /// set.insert("b".to_string());
1379 ///
1380 /// // Not possible to collect to a Vec<String> with a regular `.iter()`.
1381 /// let v: Vec<String> = set.into_iter().collect();
1382 ///
1383 /// // Will print in an arbitrary order.
62682a34 1384 /// for x in &v {
9346a6ac
AL
1385 /// println!("{}", x);
1386 /// }
1387 /// ```
48663c56 1388 #[inline]
85aaf69f 1389 fn into_iter(self) -> IntoIter<T> {
1b1a35ee 1390 IntoIter { base: self.base.into_iter() }
85aaf69f
SL
1391 }
1392}
1393
92a42be0 1394#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 1395impl<K> Clone for Iter<'_, K> {
48663c56 1396 #[inline]
9fa01778 1397 fn clone(&self) -> Self {
1b1a35ee 1398 Iter { base: self.base.clone() }
c30ab7b3 1399 }
c34b1796 1400}
85aaf69f 1401#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
1402impl<'a, K> Iterator for Iter<'a, K> {
1403 type Item = &'a K;
1404
48663c56 1405 #[inline]
c30ab7b3 1406 fn next(&mut self) -> Option<&'a K> {
1b1a35ee 1407 self.base.next()
c30ab7b3 1408 }
48663c56 1409 #[inline]
c30ab7b3 1410 fn size_hint(&self) -> (usize, Option<usize>) {
1b1a35ee 1411 self.base.size_hint()
c30ab7b3 1412 }
85aaf69f
SL
1413}
1414#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 1415impl<K> ExactSizeIterator for Iter<'_, K> {
48663c56 1416 #[inline]
c30ab7b3 1417 fn len(&self) -> usize {
1b1a35ee 1418 self.base.len()
c30ab7b3 1419 }
1a4d82fc 1420}
0531ce1d 1421#[stable(feature = "fused", since = "1.26.0")]
9fa01778 1422impl<K> FusedIterator for Iter<'_, K> {}
1a4d82fc 1423
8bb4bdeb 1424#[stable(feature = "std_debug", since = "1.16.0")]
9fa01778 1425impl<K: fmt::Debug> fmt::Debug for Iter<'_, K> {
532ac7d7 1426 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
cc61c64b 1427 f.debug_list().entries(self.clone()).finish()
32a655c1
SL
1428 }
1429}
1430
85aaf69f 1431#[stable(feature = "rust1", since = "1.0.0")]
1a4d82fc
JJ
1432impl<K> Iterator for IntoIter<K> {
1433 type Item = K;
1434
48663c56 1435 #[inline]
c30ab7b3 1436 fn next(&mut self) -> Option<K> {
1b1a35ee 1437 self.base.next()
c30ab7b3 1438 }
48663c56 1439 #[inline]
c30ab7b3 1440 fn size_hint(&self) -> (usize, Option<usize>) {
1b1a35ee 1441 self.base.size_hint()
c30ab7b3 1442 }
85aaf69f
SL
1443}
1444#[stable(feature = "rust1", since = "1.0.0")]
1445impl<K> ExactSizeIterator for IntoIter<K> {
48663c56 1446 #[inline]
c30ab7b3 1447 fn len(&self) -> usize {
1b1a35ee 1448 self.base.len()
c30ab7b3 1449 }
1a4d82fc 1450}
0531ce1d 1451#[stable(feature = "fused", since = "1.26.0")]
9e0c209e 1452impl<K> FusedIterator for IntoIter<K> {}
1a4d82fc 1453
8bb4bdeb 1454#[stable(feature = "std_debug", since = "1.16.0")]
32a655c1 1455impl<K: fmt::Debug> fmt::Debug for IntoIter<K> {
532ac7d7 1456 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1b1a35ee 1457 fmt::Debug::fmt(&self.base, f)
32a655c1
SL
1458 }
1459}
1460
85aaf69f
SL
1461#[stable(feature = "rust1", since = "1.0.0")]
1462impl<'a, K> Iterator for Drain<'a, K> {
1a4d82fc
JJ
1463 type Item = K;
1464
48663c56 1465 #[inline]
c30ab7b3 1466 fn next(&mut self) -> Option<K> {
1b1a35ee 1467 self.base.next()
c30ab7b3 1468 }
48663c56 1469 #[inline]
c30ab7b3 1470 fn size_hint(&self) -> (usize, Option<usize>) {
1b1a35ee 1471 self.base.size_hint()
c30ab7b3 1472 }
85aaf69f
SL
1473}
1474#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 1475impl<K> ExactSizeIterator for Drain<'_, K> {
48663c56 1476 #[inline]
c30ab7b3 1477 fn len(&self) -> usize {
1b1a35ee 1478 self.base.len()
c30ab7b3 1479 }
1a4d82fc 1480}
0531ce1d 1481#[stable(feature = "fused", since = "1.26.0")]
9fa01778 1482impl<K> FusedIterator for Drain<'_, K> {}
1a4d82fc 1483
8bb4bdeb 1484#[stable(feature = "std_debug", since = "1.16.0")]
9fa01778 1485impl<K: fmt::Debug> fmt::Debug for Drain<'_, K> {
532ac7d7 1486 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1b1a35ee
XL
1487 fmt::Debug::fmt(&self.base, f)
1488 }
1489}
1490
1491#[unstable(feature = "hash_drain_filter", issue = "59618")]
1492impl<K, F> Iterator for DrainFilter<'_, K, F>
1493where
1494 F: FnMut(&K) -> bool,
1495{
1496 type Item = K;
1497
1498 #[inline]
1499 fn next(&mut self) -> Option<K> {
1500 self.base.next()
1501 }
1502 #[inline]
1503 fn size_hint(&self) -> (usize, Option<usize>) {
1504 self.base.size_hint()
1505 }
1506}
1507
1508#[unstable(feature = "hash_drain_filter", issue = "59618")]
1509impl<K, F> FusedIterator for DrainFilter<'_, K, F> where F: FnMut(&K) -> bool {}
1510
1511#[unstable(feature = "hash_drain_filter", issue = "59618")]
1512impl<'a, K, F> fmt::Debug for DrainFilter<'a, K, F>
1513where
1514 F: FnMut(&K) -> bool,
1515{
1516 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1517 f.pad("DrainFilter { .. }")
32a655c1
SL
1518 }
1519}
1520
92a42be0 1521#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 1522impl<T, S> Clone for Intersection<'_, T, S> {
48663c56 1523 #[inline]
9fa01778 1524 fn clone(&self) -> Self {
c34b1796
AL
1525 Intersection { iter: self.iter.clone(), ..*self }
1526 }
1527}
1528
85aaf69f
SL
1529#[stable(feature = "rust1", since = "1.0.0")]
1530impl<'a, T, S> Iterator for Intersection<'a, T, S>
dfeec247
XL
1531where
1532 T: Eq + Hash,
1533 S: BuildHasher,
1a4d82fc
JJ
1534{
1535 type Item = &'a T;
1536
48663c56 1537 #[inline]
1a4d82fc
JJ
1538 fn next(&mut self) -> Option<&'a T> {
1539 loop {
ff7c6d11
XL
1540 let elt = self.iter.next()?;
1541 if self.other.contains(elt) {
1542 return Some(elt);
1a4d82fc
JJ
1543 }
1544 }
1545 }
1546
48663c56 1547 #[inline]
85aaf69f 1548 fn size_hint(&self) -> (usize, Option<usize>) {
1a4d82fc
JJ
1549 let (_, upper) = self.iter.size_hint();
1550 (0, upper)
1551 }
1552}
1553
8bb4bdeb 1554#[stable(feature = "std_debug", since = "1.16.0")]
9fa01778 1555impl<T, S> fmt::Debug for Intersection<'_, T, S>
dfeec247
XL
1556where
1557 T: fmt::Debug + Eq + Hash,
1558 S: BuildHasher,
32a655c1 1559{
532ac7d7 1560 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
cc61c64b 1561 f.debug_list().entries(self.clone()).finish()
32a655c1
SL
1562 }
1563}
1564
0531ce1d 1565#[stable(feature = "fused", since = "1.26.0")]
9fa01778 1566impl<T, S> FusedIterator for Intersection<'_, T, S>
dfeec247
XL
1567where
1568 T: Eq + Hash,
1569 S: BuildHasher,
c30ab7b3
SL
1570{
1571}
9e0c209e 1572
92a42be0 1573#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 1574impl<T, S> Clone for Difference<'_, T, S> {
48663c56 1575 #[inline]
9fa01778 1576 fn clone(&self) -> Self {
c34b1796
AL
1577 Difference { iter: self.iter.clone(), ..*self }
1578 }
1579}
1580
85aaf69f
SL
1581#[stable(feature = "rust1", since = "1.0.0")]
1582impl<'a, T, S> Iterator for Difference<'a, T, S>
dfeec247
XL
1583where
1584 T: Eq + Hash,
1585 S: BuildHasher,
1a4d82fc
JJ
1586{
1587 type Item = &'a T;
1588
48663c56 1589 #[inline]
1a4d82fc
JJ
1590 fn next(&mut self) -> Option<&'a T> {
1591 loop {
ff7c6d11
XL
1592 let elt = self.iter.next()?;
1593 if !self.other.contains(elt) {
1594 return Some(elt);
1a4d82fc
JJ
1595 }
1596 }
1597 }
1598
48663c56 1599 #[inline]
85aaf69f 1600 fn size_hint(&self) -> (usize, Option<usize>) {
1a4d82fc
JJ
1601 let (_, upper) = self.iter.size_hint();
1602 (0, upper)
1603 }
1604}
1605
0531ce1d 1606#[stable(feature = "fused", since = "1.26.0")]
9fa01778 1607impl<T, S> FusedIterator for Difference<'_, T, S>
dfeec247
XL
1608where
1609 T: Eq + Hash,
1610 S: BuildHasher,
c30ab7b3
SL
1611{
1612}
9e0c209e 1613
8bb4bdeb 1614#[stable(feature = "std_debug", since = "1.16.0")]
9fa01778 1615impl<T, S> fmt::Debug for Difference<'_, T, S>
dfeec247
XL
1616where
1617 T: fmt::Debug + Eq + Hash,
1618 S: BuildHasher,
32a655c1 1619{
532ac7d7 1620 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
cc61c64b 1621 f.debug_list().entries(self.clone()).finish()
32a655c1
SL
1622 }
1623}
1624
92a42be0 1625#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 1626impl<T, S> Clone for SymmetricDifference<'_, T, S> {
48663c56 1627 #[inline]
9fa01778 1628 fn clone(&self) -> Self {
c34b1796
AL
1629 SymmetricDifference { iter: self.iter.clone() }
1630 }
1631}
1632
85aaf69f
SL
1633#[stable(feature = "rust1", since = "1.0.0")]
1634impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S>
dfeec247
XL
1635where
1636 T: Eq + Hash,
1637 S: BuildHasher,
1a4d82fc
JJ
1638{
1639 type Item = &'a T;
1640
48663c56 1641 #[inline]
c30ab7b3
SL
1642 fn next(&mut self) -> Option<&'a T> {
1643 self.iter.next()
1644 }
48663c56 1645 #[inline]
c30ab7b3
SL
1646 fn size_hint(&self) -> (usize, Option<usize>) {
1647 self.iter.size_hint()
1648 }
1a4d82fc
JJ
1649}
1650
0531ce1d 1651#[stable(feature = "fused", since = "1.26.0")]
9fa01778 1652impl<T, S> FusedIterator for SymmetricDifference<'_, T, S>
dfeec247
XL
1653where
1654 T: Eq + Hash,
1655 S: BuildHasher,
c30ab7b3
SL
1656{
1657}
9e0c209e 1658
8bb4bdeb 1659#[stable(feature = "std_debug", since = "1.16.0")]
9fa01778 1660impl<T, S> fmt::Debug for SymmetricDifference<'_, T, S>
dfeec247
XL
1661where
1662 T: fmt::Debug + Eq + Hash,
1663 S: BuildHasher,
32a655c1 1664{
532ac7d7 1665 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
cc61c64b 1666 f.debug_list().entries(self.clone()).finish()
32a655c1
SL
1667 }
1668}
1669
92a42be0 1670#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 1671impl<T, S> Clone for Union<'_, T, S> {
48663c56 1672 #[inline]
9fa01778 1673 fn clone(&self) -> Self {
c30ab7b3
SL
1674 Union { iter: self.iter.clone() }
1675 }
c34b1796
AL
1676}
1677
0531ce1d 1678#[stable(feature = "fused", since = "1.26.0")]
9fa01778 1679impl<T, S> FusedIterator for Union<'_, T, S>
dfeec247
XL
1680where
1681 T: Eq + Hash,
1682 S: BuildHasher,
c30ab7b3
SL
1683{
1684}
9e0c209e 1685
8bb4bdeb 1686#[stable(feature = "std_debug", since = "1.16.0")]
9fa01778 1687impl<T, S> fmt::Debug for Union<'_, T, S>
dfeec247
XL
1688where
1689 T: fmt::Debug + Eq + Hash,
1690 S: BuildHasher,
32a655c1 1691{
532ac7d7 1692 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
cc61c64b 1693 f.debug_list().entries(self.clone()).finish()
32a655c1
SL
1694 }
1695}
1696
85aaf69f
SL
1697#[stable(feature = "rust1", since = "1.0.0")]
1698impl<'a, T, S> Iterator for Union<'a, T, S>
dfeec247
XL
1699where
1700 T: Eq + Hash,
1701 S: BuildHasher,
1a4d82fc
JJ
1702{
1703 type Item = &'a T;
1704
48663c56 1705 #[inline]
c30ab7b3
SL
1706 fn next(&mut self) -> Option<&'a T> {
1707 self.iter.next()
1708 }
48663c56 1709 #[inline]
c30ab7b3
SL
1710 fn size_hint(&self) -> (usize, Option<usize>) {
1711 self.iter.size_hint()
1712 }
1a4d82fc
JJ
1713}
1714
54a0048b
SL
1715#[allow(dead_code)]
1716fn assert_covariance() {
c30ab7b3
SL
1717 fn set<'new>(v: HashSet<&'static str>) -> HashSet<&'new str> {
1718 v
1719 }
1720 fn iter<'a, 'new>(v: Iter<'a, &'static str>) -> Iter<'a, &'new str> {
1721 v
1722 }
1723 fn into_iter<'new>(v: IntoIter<&'static str>) -> IntoIter<&'new str> {
1724 v
1725 }
dfeec247
XL
1726 fn difference<'a, 'new>(
1727 v: Difference<'a, &'static str, RandomState>,
1728 ) -> Difference<'a, &'new str, RandomState> {
c30ab7b3
SL
1729 v
1730 }
dfeec247
XL
1731 fn symmetric_difference<'a, 'new>(
1732 v: SymmetricDifference<'a, &'static str, RandomState>,
1733 ) -> SymmetricDifference<'a, &'new str, RandomState> {
c30ab7b3
SL
1734 v
1735 }
dfeec247
XL
1736 fn intersection<'a, 'new>(
1737 v: Intersection<'a, &'static str, RandomState>,
1738 ) -> Intersection<'a, &'new str, RandomState> {
c30ab7b3
SL
1739 v
1740 }
dfeec247
XL
1741 fn union<'a, 'new>(
1742 v: Union<'a, &'static str, RandomState>,
1743 ) -> Union<'a, &'new str, RandomState> {
c30ab7b3
SL
1744 v
1745 }
1746 fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> {
1747 d
1748 }
54a0048b 1749}