@@ -123,13 +123,13 @@ pub struct HashSet<T, S = RandomState> {
123
123
}
124
124
125
125
impl < T : Hash + Eq > HashSet < T , RandomState > {
126
- /// Creates an empty HashSet.
126
+ /// Creates an empty ` HashSet` .
127
127
///
128
128
/// # Examples
129
129
///
130
130
/// ```
131
131
/// use std::collections::HashSet;
132
- /// let mut set: HashSet<i32> = HashSet::new();
132
+ /// let set: HashSet<i32> = HashSet::new();
133
133
/// ```
134
134
#[ inline]
135
135
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -146,7 +146,8 @@ impl<T: Hash + Eq> HashSet<T, RandomState> {
146
146
///
147
147
/// ```
148
148
/// 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);
150
151
/// ```
151
152
#[ inline]
152
153
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -215,6 +216,17 @@ impl<T, S> HashSet<T, S>
215
216
/// Returns a reference to the set's [`BuildHasher`].
216
217
///
217
218
/// [`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
+ /// ```
218
230
#[ stable( feature = "hashmap_public_hasher" , since = "1.9.0" ) ]
219
231
pub fn hasher ( & self ) -> & S {
220
232
self . map . hasher ( )
@@ -249,6 +261,7 @@ impl<T, S> HashSet<T, S>
249
261
/// use std::collections::HashSet;
250
262
/// let mut set: HashSet<i32> = HashSet::new();
251
263
/// set.reserve(10);
264
+ /// assert!(set.capacity() >= 10);
252
265
/// ```
253
266
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
254
267
pub fn reserve ( & mut self , additional : usize ) {
@@ -312,13 +325,13 @@ impl<T, S> HashSet<T, S>
312
325
/// println!("{}", x); // Print 1
313
326
/// }
314
327
///
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());
317
330
///
318
331
/// // Note that difference is not symmetric,
319
332
/// // 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());
322
335
/// ```
323
336
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
324
337
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>
343
356
/// println!("{}", x);
344
357
/// }
345
358
///
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();
348
361
///
349
362
/// assert_eq!(diff1, diff2);
350
- /// assert_eq!(diff1, [1, 4].iter().cloned(). collect());
363
+ /// assert_eq!(diff1, [1, 4].iter().collect());
351
364
/// ```
352
365
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
353
366
pub fn symmetric_difference < ' a > ( & ' a self ,
@@ -371,8 +384,8 @@ impl<T, S> HashSet<T, S>
371
384
/// println!("{}", x);
372
385
/// }
373
386
///
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());
376
389
/// ```
377
390
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
378
391
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>
397
410
/// println!("{}", x);
398
411
/// }
399
412
///
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());
402
415
/// ```
403
416
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
404
417
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>
440
453
}
441
454
442
455
/// 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
+ /// ```
443
472
#[ inline]
444
473
#[ stable( feature = "drain" , since = "1.6.0" ) ]
445
474
pub fn drain ( & mut self ) -> Drain < T > {
0 commit comments