@@ -956,8 +956,8 @@ where
956
956
}
957
957
}
958
958
}
959
- /// Retains only the elements specified by the predicate, and returns an iterator over the
960
- /// removed items.
959
+ /// Drains elements which are false under the given predicate,
960
+ /// and returns an iterator over the removed items.
961
961
///
962
962
/// In other words, move all pairs `(k, v)` such that `f(&k,&mut v)` returns `false` out
963
963
/// into another iterator.
@@ -972,7 +972,8 @@ where
972
972
/// assert_eq!(drained.count(), 4);
973
973
/// assert_eq!(map.len(), 4);
974
974
/// ```
975
- pub fn drain_filter < ' a , F > ( & ' a mut self , mut f : F ) -> impl Iterator < Item =( K , V ) > + ' _
975
+ #[ must_use]
976
+ pub fn drain_filter < ' a , F > ( & ' a mut self , mut f : F ) -> impl Iterator < Item = ( K , V ) > + ' _
976
977
where
977
978
F : ' a + FnMut ( & K , & mut V ) -> bool ,
978
979
{
@@ -3526,10 +3527,12 @@ mod test_map {
3526
3527
3527
3528
#[ test]
3528
3529
fn test_drain_filter ( ) {
3529
- let mut map: HashMap < i32 , i32 > = ( 0 ..8 ) . map ( |x|( x, x* 10 ) ) . collect ( ) ;
3530
- let drained = map. drain_filter ( |& k, _| k % 2 == 0 ) ;
3531
- assert_eq ! ( drained. count( ) , 4 ) ;
3532
- assert_eq ! ( map. len( ) , 4 ) ;
3530
+ let mut map: HashMap < i32 , i32 > = ( 0 ..8 ) . map ( |x| ( x, x * 10 ) ) . collect ( ) ;
3531
+ let drained = map. drain_filter ( |& k, _| k % 2 == 0 ) ;
3532
+ let mut out = drained. collect :: < Vec < _ > > ( ) ;
3533
+ out. sort_unstable ( ) ;
3534
+ assert_eq ! ( vec![ ( 1 , 10 ) , ( 3 , 30 ) , ( 5 , 50 ) , ( 7 , 70 ) ] , out) ;
3535
+ assert_eq ! ( map. len( ) , 4 ) ;
3533
3536
}
3534
3537
3535
3538
#[ test]
0 commit comments