@@ -623,28 +623,27 @@ impl<K, V, S> HashMap<K, V, S> {
623
623
/// If the closure returns false, or panics, the element remains in the map and will not be
624
624
/// yielded.
625
625
///
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
627
627
/// whether you choose to keep or remove it.
628
628
///
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.
631
632
///
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
635
634
///
636
635
/// # Examples
637
636
///
638
637
/// Splitting a map into even and odd keys, reusing the original map:
639
638
///
640
639
/// ```
641
- /// #![feature(hash_drain_filter )]
640
+ /// #![feature(hash_extract_if )]
642
641
/// use std::collections::HashMap;
643
642
///
644
643
/// 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();
646
645
///
647
- /// let mut evens = drained .keys().copied().collect::<Vec<_>>();
646
+ /// let mut evens = extracted .keys().copied().collect::<Vec<_>>();
648
647
/// let mut odds = map.keys().copied().collect::<Vec<_>>();
649
648
/// evens.sort();
650
649
/// odds.sort();
@@ -654,12 +653,12 @@ impl<K, V, S> HashMap<K, V, S> {
654
653
/// ```
655
654
#[ inline]
656
655
#[ 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 >
659
658
where
660
659
F : FnMut ( & K , & mut V ) -> bool ,
661
660
{
662
- DrainFilter { base : self . base . drain_filter ( pred) }
661
+ ExtractIf { base : self . base . extract_if ( pred) }
663
662
}
664
663
665
664
/// Retains only the elements specified by the predicate.
@@ -1578,28 +1577,29 @@ impl<'a, K, V> Drain<'a, K, V> {
1578
1577
1579
1578
/// A draining, filtering iterator over the entries of a `HashMap`.
1580
1579
///
1581
- /// This `struct` is created by the [`drain_filter `] method on [`HashMap`].
1580
+ /// This `struct` is created by the [`extract_if `] method on [`HashMap`].
1582
1581
///
1583
- /// [`drain_filter `]: HashMap::drain_filter
1582
+ /// [`extract_if `]: HashMap::extract_if
1584
1583
///
1585
1584
/// # Example
1586
1585
///
1587
1586
/// ```
1588
- /// #![feature(hash_drain_filter )]
1587
+ /// #![feature(hash_extract_if )]
1589
1588
///
1590
1589
/// use std::collections::HashMap;
1591
1590
///
1592
1591
/// let mut map = HashMap::from([
1593
1592
/// ("a", 1),
1594
1593
/// ]);
1595
- /// let iter = map.drain_filter (|_k, v| *v % 2 == 0);
1594
+ /// let iter = map.extract_if (|_k, v| *v % 2 == 0);
1596
1595
/// ```
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 >
1599
1599
where
1600
1600
F : FnMut ( & K , & mut V ) -> bool ,
1601
1601
{
1602
- base : base:: DrainFilter < ' a , K , V , F > ,
1602
+ base : base:: ExtractIf < ' a , K , V , F > ,
1603
1603
}
1604
1604
1605
1605
/// A mutable iterator over the values of a `HashMap`.
@@ -2479,8 +2479,8 @@ where
2479
2479
}
2480
2480
}
2481
2481
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 >
2484
2484
where
2485
2485
F : FnMut ( & K , & mut V ) -> bool ,
2486
2486
{
@@ -2496,16 +2496,16 @@ where
2496
2496
}
2497
2497
}
2498
2498
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 { }
2501
2501
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 >
2504
2504
where
2505
2505
F : FnMut ( & K , & mut V ) -> bool ,
2506
2506
{
2507
2507
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 ( )
2509
2509
}
2510
2510
}
2511
2511
0 commit comments