@@ -972,10 +972,10 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
972972 /// In other words, move all pairs `(k, v)` such that `f(&k, &mut v)` returns `true` out
973973 /// into another iterator.
974974 ///
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
976976 /// whether you choose to keep or remove it.
977977 ///
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
979979 /// or the iteration short-circuits, then the remaining elements will be retained.
980980 /// Use [`retain()`] with a negated predicate if you do not need the returned iterator.
981981 ///
@@ -988,7 +988,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
988988 ///
989989 /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
990990 ///
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();
992992 ///
993993 /// let mut evens = drained.keys().cloned().collect::<Vec<_>>();
994994 /// let mut odds = map.keys().cloned().collect::<Vec<_>>();
@@ -1001,20 +1001,20 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
10011001 /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
10021002 ///
10031003 /// { // 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);
10051005 /// }
10061006 ///
1007- /// // DrainFilter was not exhausted, therefore no elements were drained.
1007+ /// // ExtractIf was not exhausted, therefore no elements were drained.
10081008 /// assert_eq!(map.len(), 8);
10091009 /// ```
10101010 #[ 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 >
10121012 where
10131013 F : FnMut ( & K , & mut V ) -> bool ,
10141014 {
1015- DrainFilter {
1015+ ExtractIf {
10161016 f,
1017- inner : DrainFilterInner {
1017+ inner : ExtractIfInner {
10181018 iter : unsafe { self . table . iter ( ) } ,
10191019 table : & mut self . table ,
10201020 } ,
@@ -2707,10 +2707,10 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
27072707/// A draining iterator over entries of a `HashMap` which don't satisfy the predicate
27082708/// `f(&k, &mut v)` in arbitrary order. The iterator element type is `(K, V)`.
27092709///
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
27112711/// documentation for more.
27122712///
2713- /// [`drain_filter `]: struct.HashMap.html#method.drain_filter
2713+ /// [`extract_if `]: struct.HashMap.html#method.extract_if
27142714/// [`HashMap`]: struct.HashMap.html
27152715///
27162716/// # Examples
@@ -2720,31 +2720,31 @@ impl<K, V, A: Allocator + Clone> Drain<'_, K, V, A> {
27202720///
27212721/// let mut map: HashMap<i32, &str> = [(1, "a"), (2, "b"), (3, "c")].into();
27222722///
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()];
27252725///
2726- /// // The `DrainFilter ` iterator produces items in arbitrary order, so the
2726+ /// // The `ExtractIf ` iterator produces items in arbitrary order, so the
27272727/// // items must be sorted to test them against a sorted array.
27282728/// vec.sort_unstable();
27292729/// assert_eq!(vec, [Some((1, "a")),Some((3, "c"))]);
27302730///
27312731/// // 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 );
27352735///
27362736/// assert_eq!(map.len(), 1);
27372737/// ```
27382738#[ 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 >
27402740where
27412741 F : FnMut ( & K , & mut V ) -> bool ,
27422742{
27432743 f : F ,
2744- inner : DrainFilterInner < ' a , K , V , A > ,
2744+ inner : ExtractIfInner < ' a , K , V , A > ,
27452745}
27462746
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 >
27482748where
27492749 F : FnMut ( & K , & mut V ) -> bool ,
27502750 A : Allocator + Clone ,
@@ -2762,15 +2762,15 @@ where
27622762 }
27632763}
27642764
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 { }
27662766
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 > {
27692769 pub iter : RawIter < ( K , V ) > ,
27702770 pub table : & ' a mut RawTable < ( K , V ) , A > ,
27712771}
27722772
2773- impl < K , V , A : Allocator + Clone > DrainFilterInner < ' _ , K , V , A > {
2773+ impl < K , V , A : Allocator + Clone > ExtractIfInner < ' _ , K , V , A > {
27742774 #[ cfg_attr( feature = "inline-more" , inline) ]
27752775 pub ( super ) fn next < F > ( & mut self , f : & mut F ) -> Option < ( K , V ) >
27762776 where
@@ -8121,18 +8121,18 @@ mod test_map {
81218121 }
81228122
81238123 #[ test]
8124- fn test_drain_filter ( ) {
8124+ fn test_extract_if ( ) {
81258125 {
81268126 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 ) ;
81288128 let mut out = drained. collect :: < Vec < _ > > ( ) ;
81298129 out. sort_unstable ( ) ;
81308130 assert_eq ! ( vec![ ( 0 , 0 ) , ( 2 , 20 ) , ( 4 , 40 ) , ( 6 , 60 ) ] , out) ;
81318131 assert_eq ! ( map. len( ) , 4 ) ;
81328132 }
81338133 {
81348134 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) ;
81368136 assert_eq ! ( map. len( ) , 4 ) ;
81378137 }
81388138 }
0 commit comments