@@ -623,28 +623,27 @@ impl<K, V, S> HashMap<K, V, S> {
623623 /// If the closure returns false, or panics, the element remains in the map and will not be
624624 /// yielded.
625625 ///
626- /// Note that `drain_filter ` lets you mutate every value in the filter closure, regardless of
626+ /// Note that `extract_if ` lets you mutate every value in the filter closure, regardless of
627627 /// whether you choose to keep or remove it.
628628 ///
629- /// If the iterator is only partially consumed or not consumed at all, each of the remaining
630- /// elements will still be subjected to the closure and removed and dropped if it returns true.
629+ /// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
630+ /// or the iteration short-circuits, then the remaining elements will be retained.
631+ /// Use [`retain`] with a negated predicate if you do not need the returned iterator.
631632 ///
632- /// It is unspecified how many more elements will be subjected to the closure
633- /// if a panic occurs in the closure, or a panic occurs while dropping an element,
634- /// or if the `DrainFilter` value is leaked.
633+ /// [`retain`]: HashMap::retain
635634 ///
636635 /// # Examples
637636 ///
638637 /// Splitting a map into even and odd keys, reusing the original map:
639638 ///
640639 /// ```
641- /// #![feature(hash_drain_filter )]
640+ /// #![feature(hash_extract_if )]
642641 /// use std::collections::HashMap;
643642 ///
644643 /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
645- /// let drained : HashMap<i32, i32> = map.drain_filter (|k, _v| k % 2 == 0).collect();
644+ /// let extracted : HashMap<i32, i32> = map.extract_if (|k, _v| k % 2 == 0).collect();
646645 ///
647- /// let mut evens = drained .keys().copied().collect::<Vec<_>>();
646+ /// let mut evens = extracted .keys().copied().collect::<Vec<_>>();
648647 /// let mut odds = map.keys().copied().collect::<Vec<_>>();
649648 /// evens.sort();
650649 /// odds.sort();
@@ -654,12 +653,12 @@ impl<K, V, S> HashMap<K, V, S> {
654653 /// ```
655654 #[ inline]
656655 #[ rustc_lint_query_instability]
657- #[ unstable( feature = "hash_drain_filter " , issue = "59618" ) ]
658- pub fn drain_filter < F > ( & mut self , pred : F ) -> DrainFilter < ' _ , K , V , F >
656+ #[ unstable( feature = "hash_extract_if " , issue = "59618" ) ]
657+ pub fn extract_if < F > ( & mut self , pred : F ) -> ExtractIf < ' _ , K , V , F >
659658 where
660659 F : FnMut ( & K , & mut V ) -> bool ,
661660 {
662- DrainFilter { base : self . base . drain_filter ( pred) }
661+ ExtractIf { base : self . base . extract_if ( pred) }
663662 }
664663
665664 /// Retains only the elements specified by the predicate.
@@ -1578,28 +1577,29 @@ impl<'a, K, V> Drain<'a, K, V> {
15781577
15791578/// A draining, filtering iterator over the entries of a `HashMap`.
15801579///
1581- /// This `struct` is created by the [`drain_filter `] method on [`HashMap`].
1580+ /// This `struct` is created by the [`extract_if `] method on [`HashMap`].
15821581///
1583- /// [`drain_filter `]: HashMap::drain_filter
1582+ /// [`extract_if `]: HashMap::extract_if
15841583///
15851584/// # Example
15861585///
15871586/// ```
1588- /// #![feature(hash_drain_filter )]
1587+ /// #![feature(hash_extract_if )]
15891588///
15901589/// use std::collections::HashMap;
15911590///
15921591/// let mut map = HashMap::from([
15931592/// ("a", 1),
15941593/// ]);
1595- /// let iter = map.drain_filter (|_k, v| *v % 2 == 0);
1594+ /// let iter = map.extract_if (|_k, v| *v % 2 == 0);
15961595/// ```
1597- #[ unstable( feature = "hash_drain_filter" , issue = "59618" ) ]
1598- pub struct DrainFilter < ' a , K , V , F >
1596+ #[ unstable( feature = "hash_extract_if" , issue = "59618" ) ]
1597+ #[ must_use = "iterators are lazy and do nothing unless consumed" ]
1598+ pub struct ExtractIf < ' a , K , V , F >
15991599where
16001600 F : FnMut ( & K , & mut V ) -> bool ,
16011601{
1602- base : base:: DrainFilter < ' a , K , V , F > ,
1602+ base : base:: ExtractIf < ' a , K , V , F > ,
16031603}
16041604
16051605/// A mutable iterator over the values of a `HashMap`.
@@ -2479,8 +2479,8 @@ where
24792479 }
24802480}
24812481
2482- #[ unstable( feature = "hash_drain_filter " , issue = "59618" ) ]
2483- impl < K , V , F > Iterator for DrainFilter < ' _ , K , V , F >
2482+ #[ unstable( feature = "hash_extract_if " , issue = "59618" ) ]
2483+ impl < K , V , F > Iterator for ExtractIf < ' _ , K , V , F >
24842484where
24852485 F : FnMut ( & K , & mut V ) -> bool ,
24862486{
@@ -2496,16 +2496,16 @@ where
24962496 }
24972497}
24982498
2499- #[ unstable( feature = "hash_drain_filter " , issue = "59618" ) ]
2500- impl < K , V , F > FusedIterator for DrainFilter < ' _ , K , V , F > where F : FnMut ( & K , & mut V ) -> bool { }
2499+ #[ unstable( feature = "hash_extract_if " , issue = "59618" ) ]
2500+ impl < K , V , F > FusedIterator for ExtractIf < ' _ , K , V , F > where F : FnMut ( & K , & mut V ) -> bool { }
25012501
2502- #[ unstable( feature = "hash_drain_filter " , issue = "59618" ) ]
2503- impl < ' a , K , V , F > fmt:: Debug for DrainFilter < ' a , K , V , F >
2502+ #[ unstable( feature = "hash_extract_if " , issue = "59618" ) ]
2503+ impl < ' a , K , V , F > fmt:: Debug for ExtractIf < ' a , K , V , F >
25042504where
25052505 F : FnMut ( & K , & mut V ) -> bool ,
25062506{
25072507 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2508- f. debug_struct ( "DrainFilter " ) . finish_non_exhaustive ( )
2508+ f. debug_struct ( "ExtractIf " ) . finish_non_exhaustive ( )
25092509 }
25102510}
25112511
0 commit comments