@@ -584,24 +584,30 @@ impl<K, V, S> HashMap<K, V, S> {
584
584
}
585
585
}
586
586
587
- /// Drains elements which are false under the given predicate,
587
+ /// Drains elements which are true under the given predicate,
588
588
/// and returns an iterator over the removed items.
589
589
///
590
- /// In other words, move all pairs `(k, v)` such that `f(&k,&mut v)` returns `false ` out
590
+ /// In other words, move all pairs `(k, v)` such that `f(&k,&mut v)` returns `true ` out
591
591
/// into another iterator.
592
592
///
593
- /// When the returned DrainedFilter is dropped, the elements that don't satisfy
593
+ /// When the returned DrainedFilter is dropped, any remaining elements that satisfy
594
594
/// the predicate are dropped from the table.
595
595
///
596
596
/// # Examples
597
597
///
598
598
/// ```
599
599
/// use hashbrown::HashMap;
600
600
///
601
- /// let mut map: HashMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
602
- /// let drained = map.drain_filter(|&k, _| k % 2 == 0);
603
- /// assert_eq!(drained.count(), 4);
604
- /// assert_eq!(map.len(), 4);
601
+ /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
602
+ /// let drained: HashMap<i32, i32> = map.drain_filter(|k, _v| k % 2 == 0).collect();
603
+ ///
604
+ /// let mut evens = drained.keys().copied().collect::<Vec<_>>();
605
+ /// let mut odds = map.keys().copied().collect::<Vec<_>>();
606
+ /// evens.sort();
607
+ /// odds.sort();
608
+ ///
609
+ /// assert_eq!(evens, vec![0, 2, 4, 6]);
610
+ /// assert_eq!(odds, vec![1, 3, 5, 7]);
605
611
/// ```
606
612
#[ cfg_attr( feature = "inline-more" , inline) ]
607
613
pub fn drain_filter < F > ( & mut self , f : F ) -> DrainFilter < ' _ , K , V , F >
@@ -1401,7 +1407,7 @@ impl<K, V> DrainFilterInner<'_, K, V> {
1401
1407
unsafe {
1402
1408
while let Some ( item) = self . iter . next ( ) {
1403
1409
let & mut ( ref key, ref mut value) = item. as_mut ( ) ;
1404
- if ! f ( key, value) {
1410
+ if f ( key, value) {
1405
1411
return Some ( self . table . remove ( item) ) ;
1406
1412
}
1407
1413
}
0 commit comments