Skip to content

Commit 08c0bbd

Browse files
authored
Rollup merge of #43585 - frewsxcv:frewsxcv-hashset-docs, r=steveklabnik,quietmisdreavus
Improve docs & doc examples for HashSet. None
2 parents c96ce40 + d9df296 commit 08c0bbd

File tree

1 file changed

+43
-14
lines changed
  • src/libstd/collections/hash

1 file changed

+43
-14
lines changed

src/libstd/collections/hash/set.rs

+43-14
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ pub struct HashSet<T, S = RandomState> {
123123
}
124124

125125
impl<T: Hash + Eq> HashSet<T, RandomState> {
126-
/// Creates an empty HashSet.
126+
/// Creates an empty `HashSet`.
127127
///
128128
/// # Examples
129129
///
130130
/// ```
131131
/// use std::collections::HashSet;
132-
/// let mut set: HashSet<i32> = HashSet::new();
132+
/// let set: HashSet<i32> = HashSet::new();
133133
/// ```
134134
#[inline]
135135
#[stable(feature = "rust1", since = "1.0.0")]
@@ -146,7 +146,8 @@ impl<T: Hash + Eq> HashSet<T, RandomState> {
146146
///
147147
/// ```
148148
/// use std::collections::HashSet;
149-
/// let mut set: HashSet<i32> = HashSet::with_capacity(10);
149+
/// let set: HashSet<i32> = HashSet::with_capacity(10);
150+
/// assert!(set.capacity() >= 10);
150151
/// ```
151152
#[inline]
152153
#[stable(feature = "rust1", since = "1.0.0")]
@@ -215,6 +216,17 @@ impl<T, S> HashSet<T, S>
215216
/// Returns a reference to the set's [`BuildHasher`].
216217
///
217218
/// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html
219+
///
220+
/// # Examples
221+
///
222+
/// ```
223+
/// use std::collections::HashSet;
224+
/// use std::collections::hash_map::RandomState;
225+
///
226+
/// let hasher = RandomState::new();
227+
/// let set: HashSet<i32> = HashSet::with_hasher(hasher);
228+
/// let hasher: &RandomState = set.hasher();
229+
/// ```
218230
#[stable(feature = "hashmap_public_hasher", since = "1.9.0")]
219231
pub fn hasher(&self) -> &S {
220232
self.map.hasher()
@@ -249,6 +261,7 @@ impl<T, S> HashSet<T, S>
249261
/// use std::collections::HashSet;
250262
/// let mut set: HashSet<i32> = HashSet::new();
251263
/// set.reserve(10);
264+
/// assert!(set.capacity() >= 10);
252265
/// ```
253266
#[stable(feature = "rust1", since = "1.0.0")]
254267
pub fn reserve(&mut self, additional: usize) {
@@ -312,13 +325,13 @@ impl<T, S> HashSet<T, S>
312325
/// println!("{}", x); // Print 1
313326
/// }
314327
///
315-
/// let diff: HashSet<_> = a.difference(&b).cloned().collect();
316-
/// assert_eq!(diff, [1].iter().cloned().collect());
328+
/// let diff: HashSet<_> = a.difference(&b).collect();
329+
/// assert_eq!(diff, [1].iter().collect());
317330
///
318331
/// // Note that difference is not symmetric,
319332
/// // and `b - a` means something else:
320-
/// let diff: HashSet<_> = b.difference(&a).cloned().collect();
321-
/// assert_eq!(diff, [4].iter().cloned().collect());
333+
/// let diff: HashSet<_> = b.difference(&a).collect();
334+
/// assert_eq!(diff, [4].iter().collect());
322335
/// ```
323336
#[stable(feature = "rust1", since = "1.0.0")]
324337
pub fn difference<'a>(&'a self, other: &'a HashSet<T, S>) -> Difference<'a, T, S> {
@@ -343,11 +356,11 @@ impl<T, S> HashSet<T, S>
343356
/// println!("{}", x);
344357
/// }
345358
///
346-
/// let diff1: HashSet<_> = a.symmetric_difference(&b).cloned().collect();
347-
/// let diff2: HashSet<_> = b.symmetric_difference(&a).cloned().collect();
359+
/// let diff1: HashSet<_> = a.symmetric_difference(&b).collect();
360+
/// let diff2: HashSet<_> = b.symmetric_difference(&a).collect();
348361
///
349362
/// assert_eq!(diff1, diff2);
350-
/// assert_eq!(diff1, [1, 4].iter().cloned().collect());
363+
/// assert_eq!(diff1, [1, 4].iter().collect());
351364
/// ```
352365
#[stable(feature = "rust1", since = "1.0.0")]
353366
pub fn symmetric_difference<'a>(&'a self,
@@ -371,8 +384,8 @@ impl<T, S> HashSet<T, S>
371384
/// println!("{}", x);
372385
/// }
373386
///
374-
/// let intersection: HashSet<_> = a.intersection(&b).cloned().collect();
375-
/// assert_eq!(intersection, [2, 3].iter().cloned().collect());
387+
/// let intersection: HashSet<_> = a.intersection(&b).collect();
388+
/// assert_eq!(intersection, [2, 3].iter().collect());
376389
/// ```
377390
#[stable(feature = "rust1", since = "1.0.0")]
378391
pub fn intersection<'a>(&'a self, other: &'a HashSet<T, S>) -> Intersection<'a, T, S> {
@@ -397,8 +410,8 @@ impl<T, S> HashSet<T, S>
397410
/// println!("{}", x);
398411
/// }
399412
///
400-
/// let union: HashSet<_> = a.union(&b).cloned().collect();
401-
/// assert_eq!(union, [1, 2, 3, 4].iter().cloned().collect());
413+
/// let union: HashSet<_> = a.union(&b).collect();
414+
/// assert_eq!(union, [1, 2, 3, 4].iter().collect());
402415
/// ```
403416
#[stable(feature = "rust1", since = "1.0.0")]
404417
pub fn union<'a>(&'a self, other: &'a HashSet<T, S>) -> Union<'a, T, S> {
@@ -440,6 +453,22 @@ impl<T, S> HashSet<T, S>
440453
}
441454

442455
/// Clears the set, returning all elements in an iterator.
456+
///
457+
/// # Examples
458+
///
459+
/// ```
460+
/// use std::collections::HashSet;
461+
///
462+
/// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect();
463+
/// assert!(!set.is_empty());
464+
///
465+
/// // print 1, 2, 3 in an arbitrary order
466+
/// for i in set.drain() {
467+
/// println!("{}", i);
468+
/// }
469+
///
470+
/// assert!(set.is_empty());
471+
/// ```
443472
#[inline]
444473
#[stable(feature = "drain", since = "1.6.0")]
445474
pub fn drain(&mut self) -> Drain<T> {

0 commit comments

Comments
 (0)