@@ -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
} ,
@@ -2728,10 +2728,10 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
2728
2728
/// A draining iterator over entries of a `HashMap` which don't satisfy the predicate
2729
2729
/// `f(&k, &mut v)` in arbitrary order. The iterator element type is `(K, V)`.
2730
2730
///
2731
- /// This `struct` is created by the [`drain_filter `] method on [`HashMap`]. See its
2731
+ /// This `struct` is created by the [`extract_if `] method on [`HashMap`]. See its
2732
2732
/// documentation for more.
2733
2733
///
2734
- /// [`drain_filter `]: struct.HashMap.html#method.drain_filter
2734
+ /// [`extract_if `]: struct.HashMap.html#method.extract_if
2735
2735
/// [`HashMap`]: struct.HashMap.html
2736
2736
///
2737
2737
/// # Examples
@@ -2741,31 +2741,31 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
2741
2741
///
2742
2742
/// let mut map: HashMap<i32, &str> = [(1, "a"), (2, "b"), (3, "c")].into();
2743
2743
///
2744
- /// let mut drain_filter = map.drain_filter (|k, _v| k % 2 != 0);
2745
- /// let mut vec = vec![drain_filter .next(), drain_filter .next()];
2744
+ /// let mut extract_if = map.extract_if (|k, _v| k % 2 != 0);
2745
+ /// let mut vec = vec![extract_if .next(), extract_if .next()];
2746
2746
///
2747
- /// // The `DrainFilter ` iterator produces items in arbitrary order, so the
2747
+ /// // The `ExtractIf ` iterator produces items in arbitrary order, so the
2748
2748
/// // items must be sorted to test them against a sorted array.
2749
2749
/// vec.sort_unstable();
2750
2750
/// assert_eq!(vec, [Some((1, "a")),Some((3, "c"))]);
2751
2751
///
2752
2752
/// // It is fused iterator
2753
- /// assert_eq!(drain_filter .next(), None);
2754
- /// assert_eq!(drain_filter .next(), None);
2755
- /// drop(drain_filter );
2753
+ /// assert_eq!(extract_if .next(), None);
2754
+ /// assert_eq!(extract_if .next(), None);
2755
+ /// drop(extract_if );
2756
2756
///
2757
2757
/// assert_eq!(map.len(), 1);
2758
2758
/// ```
2759
2759
#[ must_use = "Iterators are lazy unless consumed" ]
2760
- pub struct DrainFilter < ' a , K , V , F , A : Allocator + Clone = Global >
2760
+ pub struct ExtractIf < ' a , K , V , F , A : Allocator + Clone = Global >
2761
2761
where
2762
2762
F : FnMut ( & K , & mut V ) -> bool ,
2763
2763
{
2764
2764
f : F ,
2765
- inner : DrainFilterInner < ' a , K , V , A > ,
2765
+ inner : ExtractIfInner < ' a , K , V , A > ,
2766
2766
}
2767
2767
2768
- impl < K , V , F , A > Iterator for DrainFilter < ' _ , K , V , F , A >
2768
+ impl < K , V , F , A > Iterator for ExtractIf < ' _ , K , V , F , A >
2769
2769
where
2770
2770
F : FnMut ( & K , & mut V ) -> bool ,
2771
2771
A : Allocator + Clone ,
@@ -2783,15 +2783,15 @@ where
2783
2783
}
2784
2784
}
2785
2785
2786
- impl < K , V , F > FusedIterator for DrainFilter < ' _ , K , V , F > where F : FnMut ( & K , & mut V ) -> bool { }
2786
+ impl < K , V , F > FusedIterator for ExtractIf < ' _ , K , V , F > where F : FnMut ( & K , & mut V ) -> bool { }
2787
2787
2788
- /// Portions of `DrainFilter ` shared with `set::DrainFilter `
2789
- pub ( super ) struct DrainFilterInner < ' a , K , V , A : Allocator + Clone > {
2788
+ /// Portions of `ExtractIf ` shared with `set::ExtractIf `
2789
+ pub ( super ) struct ExtractIfInner < ' a , K , V , A : Allocator + Clone > {
2790
2790
pub iter : RawIter < ( K , V ) > ,
2791
2791
pub table : & ' a mut RawTable < ( K , V ) , A > ,
2792
2792
}
2793
2793
2794
- impl < K , V , A : Allocator + Clone > DrainFilterInner < ' _ , K , V , A > {
2794
+ impl < K , V , A : Allocator + Clone > ExtractIfInner < ' _ , K , V , A > {
2795
2795
#[ cfg_attr( feature = "inline-more" , inline) ]
2796
2796
pub ( super ) fn next < F > ( & mut self , f : & mut F ) -> Option < ( K , V ) >
2797
2797
where
@@ -8142,18 +8142,18 @@ mod test_map {
8142
8142
}
8143
8143
8144
8144
#[ test]
8145
- fn test_drain_filter ( ) {
8145
+ fn test_extract_if ( ) {
8146
8146
{
8147
8147
let mut map: HashMap < i32 , i32 > = ( 0 ..8 ) . map ( |x| ( x, x * 10 ) ) . collect ( ) ;
8148
- let drained = map. drain_filter ( |& k, _| k % 2 == 0 ) ;
8148
+ let drained = map. extract_if ( |& k, _| k % 2 == 0 ) ;
8149
8149
let mut out = drained. collect :: < Vec < _ > > ( ) ;
8150
8150
out. sort_unstable ( ) ;
8151
8151
assert_eq ! ( vec![ ( 0 , 0 ) , ( 2 , 20 ) , ( 4 , 40 ) , ( 6 , 60 ) ] , out) ;
8152
8152
assert_eq ! ( map. len( ) , 4 ) ;
8153
8153
}
8154
8154
{
8155
8155
let mut map: HashMap < i32 , i32 > = ( 0 ..8 ) . map ( |x| ( x, x * 10 ) ) . collect ( ) ;
8156
- map. drain_filter ( |& k, _| k % 2 == 0 ) . for_each ( drop) ;
8156
+ map. extract_if ( |& k, _| k % 2 == 0 ) . for_each ( drop) ;
8157
8157
assert_eq ! ( map. len( ) , 4 ) ;
8158
8158
}
8159
8159
}
0 commit comments