@@ -972,10 +972,10 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
972
972
/// In other words, move all pairs `(k, v)` such that `f(&k, &mut v)` returns `true` out
973
973
/// into another iterator.
974
974
///
975
- /// Note that `drain_filter ` lets you mutate every value in the filter closure, regardless of
975
+ /// Note that `extract_if ` lets you mutate every value in the filter closure, regardless of
976
976
/// whether you choose to keep or remove it.
977
977
///
978
- /// If the returned `DrainFilter ` is not exhausted, e.g. because it is dropped without iterating
978
+ /// If the returned `ExtractIf ` is not exhausted, e.g. because it is dropped without iterating
979
979
/// or the iteration short-circuits, then the remaining elements will be retained.
980
980
/// Use [`retain()`] with a negated predicate if you do not need the returned iterator.
981
981
///
@@ -988,7 +988,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
988
988
///
989
989
/// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
990
990
///
991
- /// let drained: HashMap<i32, i32> = map.drain_filter (|k, _v| k % 2 == 0).collect();
991
+ /// let drained: HashMap<i32, i32> = map.extract_if (|k, _v| k % 2 == 0).collect();
992
992
///
993
993
/// let mut evens = drained.keys().cloned().collect::<Vec<_>>();
994
994
/// let mut odds = map.keys().cloned().collect::<Vec<_>>();
@@ -1001,20 +1001,20 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
1001
1001
/// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
1002
1002
///
1003
1003
/// { // Iterator is dropped without being consumed.
1004
- /// let d = map.drain_filter (|k, _v| k % 2 != 0);
1004
+ /// let d = map.extract_if (|k, _v| k % 2 != 0);
1005
1005
/// }
1006
1006
///
1007
- /// // DrainFilter was not exhausted, therefore no elements were drained.
1007
+ /// // ExtractIf was not exhausted, therefore no elements were drained.
1008
1008
/// assert_eq!(map.len(), 8);
1009
1009
/// ```
1010
1010
#[ cfg_attr( feature = "inline-more" , inline) ]
1011
- pub fn drain_filter < F > ( & mut self , f : F ) -> DrainFilter < ' _ , K , V , F , A >
1011
+ pub fn extract_if < F > ( & mut self , f : F ) -> ExtractIf < ' _ , K , V , F , A >
1012
1012
where
1013
1013
F : FnMut ( & K , & mut V ) -> bool ,
1014
1014
{
1015
- DrainFilter {
1015
+ ExtractIf {
1016
1016
f,
1017
- inner : DrainFilterInner {
1017
+ inner : ExtractIfInner {
1018
1018
iter : unsafe { self . table . iter ( ) } ,
1019
1019
table : & mut self . table ,
1020
1020
} ,
@@ -2707,10 +2707,10 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
2707
2707
/// A draining iterator over entries of a `HashMap` which don't satisfy the predicate
2708
2708
/// `f(&k, &mut v)` in arbitrary order. The iterator element type is `(K, V)`.
2709
2709
///
2710
- /// This `struct` is created by the [`drain_filter `] method on [`HashMap`]. See its
2710
+ /// This `struct` is created by the [`extract_if `] method on [`HashMap`]. See its
2711
2711
/// documentation for more.
2712
2712
///
2713
- /// [`drain_filter `]: struct.HashMap.html#method.drain_filter
2713
+ /// [`extract_if `]: struct.HashMap.html#method.extract_if
2714
2714
/// [`HashMap`]: struct.HashMap.html
2715
2715
///
2716
2716
/// # Examples
@@ -2720,31 +2720,31 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
2720
2720
///
2721
2721
/// let mut map: HashMap<i32, &str> = [(1, "a"), (2, "b"), (3, "c")].into();
2722
2722
///
2723
- /// let mut drain_filter = map.drain_filter (|k, _v| k % 2 != 0);
2724
- /// let mut vec = vec![drain_filter .next(), drain_filter .next()];
2723
+ /// let mut extract_if = map.extract_if (|k, _v| k % 2 != 0);
2724
+ /// let mut vec = vec![extract_if .next(), extract_if .next()];
2725
2725
///
2726
- /// // The `DrainFilter ` iterator produces items in arbitrary order, so the
2726
+ /// // The `ExtractIf ` iterator produces items in arbitrary order, so the
2727
2727
/// // items must be sorted to test them against a sorted array.
2728
2728
/// vec.sort_unstable();
2729
2729
/// assert_eq!(vec, [Some((1, "a")),Some((3, "c"))]);
2730
2730
///
2731
2731
/// // It is fused iterator
2732
- /// assert_eq!(drain_filter .next(), None);
2733
- /// assert_eq!(drain_filter .next(), None);
2734
- /// drop(drain_filter );
2732
+ /// assert_eq!(extract_if .next(), None);
2733
+ /// assert_eq!(extract_if .next(), None);
2734
+ /// drop(extract_if );
2735
2735
///
2736
2736
/// assert_eq!(map.len(), 1);
2737
2737
/// ```
2738
2738
#[ must_use = "Iterators are lazy unless consumed" ]
2739
- pub struct DrainFilter < ' a , K , V , F , A : Allocator + Clone = Global >
2739
+ pub struct ExtractIf < ' a , K , V , F , A : Allocator + Clone = Global >
2740
2740
where
2741
2741
F : FnMut ( & K , & mut V ) -> bool ,
2742
2742
{
2743
2743
f : F ,
2744
- inner : DrainFilterInner < ' a , K , V , A > ,
2744
+ inner : ExtractIfInner < ' a , K , V , A > ,
2745
2745
}
2746
2746
2747
- impl < K , V , F , A > Iterator for DrainFilter < ' _ , K , V , F , A >
2747
+ impl < K , V , F , A > Iterator for ExtractIf < ' _ , K , V , F , A >
2748
2748
where
2749
2749
F : FnMut ( & K , & mut V ) -> bool ,
2750
2750
A : Allocator + Clone ,
@@ -2762,15 +2762,15 @@ where
2762
2762
}
2763
2763
}
2764
2764
2765
- impl < K , V , F > FusedIterator for DrainFilter < ' _ , K , V , F > where F : FnMut ( & K , & mut V ) -> bool { }
2765
+ impl < K , V , F > FusedIterator for ExtractIf < ' _ , K , V , F > where F : FnMut ( & K , & mut V ) -> bool { }
2766
2766
2767
- /// Portions of `DrainFilter ` shared with `set::DrainFilter `
2768
- pub ( super ) struct DrainFilterInner < ' a , K , V , A : Allocator + Clone > {
2767
+ /// Portions of `ExtractIf ` shared with `set::ExtractIf `
2768
+ pub ( super ) struct ExtractIfInner < ' a , K , V , A : Allocator + Clone > {
2769
2769
pub iter : RawIter < ( K , V ) > ,
2770
2770
pub table : & ' a mut RawTable < ( K , V ) , A > ,
2771
2771
}
2772
2772
2773
- impl < K , V , A : Allocator + Clone > DrainFilterInner < ' _ , K , V , A > {
2773
+ impl < K , V , A : Allocator + Clone > ExtractIfInner < ' _ , K , V , A > {
2774
2774
#[ cfg_attr( feature = "inline-more" , inline) ]
2775
2775
pub ( super ) fn next < F > ( & mut self , f : & mut F ) -> Option < ( K , V ) >
2776
2776
where
@@ -8121,18 +8121,18 @@ mod test_map {
8121
8121
}
8122
8122
8123
8123
#[ test]
8124
- fn test_drain_filter ( ) {
8124
+ fn test_extract_if ( ) {
8125
8125
{
8126
8126
let mut map: HashMap < i32 , i32 > = ( 0 ..8 ) . map ( |x| ( x, x * 10 ) ) . collect ( ) ;
8127
- let drained = map. drain_filter ( |& k, _| k % 2 == 0 ) ;
8127
+ let drained = map. extract_if ( |& k, _| k % 2 == 0 ) ;
8128
8128
let mut out = drained. collect :: < Vec < _ > > ( ) ;
8129
8129
out. sort_unstable ( ) ;
8130
8130
assert_eq ! ( vec![ ( 0 , 0 ) , ( 2 , 20 ) , ( 4 , 40 ) , ( 6 , 60 ) ] , out) ;
8131
8131
assert_eq ! ( map. len( ) , 4 ) ;
8132
8132
}
8133
8133
{
8134
8134
let mut map: HashMap < i32 , i32 > = ( 0 ..8 ) . map ( |x| ( x, x * 10 ) ) . collect ( ) ;
8135
- map. drain_filter ( |& k, _| k % 2 == 0 ) . for_each ( drop) ;
8135
+ map. extract_if ( |& k, _| k % 2 == 0 ) . for_each ( drop) ;
8136
8136
assert_eq ! ( map. len( ) , 4 ) ;
8137
8137
}
8138
8138
}
0 commit comments