@@ -755,6 +755,10 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
755
755
/// Clears the map, returning all key-value pairs as an iterator. Keeps the
756
756
/// allocated memory for reuse.
757
757
///
758
+ /// If the returned iterator is dropped before being fully consumed, it
759
+ /// drops the remaining key-value pairs. The returned iterator keeps a
760
+ /// mutable borrow on the vector to optimize its implementation.
761
+ ///
758
762
/// # Examples
759
763
///
760
764
/// ```
@@ -773,7 +777,18 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
773
777
/// // As we can see, the map is empty and contains no element.
774
778
/// assert!(a.is_empty() && a.len() == 0);
775
779
/// // But map capacity is equal to old one.
776
- /// assert!(a.capacity() == capacity_before_drain);
780
+ /// assert_eq!(a.capacity(), capacity_before_drain);
781
+ ///
782
+ /// let mut a = HashMap::new();
783
+ /// a.insert(1, "a");
784
+ /// a.insert(2, "b");
785
+ ///
786
+ /// { // Iterator is dropped without being consumed.
787
+ /// let d = a.drain();
788
+ /// }
789
+ ///
790
+ /// // But the map is empty even if we do not use Drain iterator.
791
+ /// assert!(a.is_empty());
777
792
/// ```
778
793
#[ cfg_attr( feature = "inline-more" , inline) ]
779
794
pub fn drain ( & mut self ) -> Drain < ' _ , K , V , A > {
@@ -785,7 +800,8 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
785
800
/// Retains only the elements specified by the predicate. Keeps the
786
801
/// allocated memory for reuse.
787
802
///
788
- /// In other words, remove all pairs `(k, v)` such that `f(&k,&mut v)` returns `false`.
803
+ /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`.
804
+ /// The elements are visited in unsorted (and unspecified) order.
789
805
///
790
806
/// # Examples
791
807
///
@@ -801,7 +817,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
801
817
/// // We can see, that the number of elements inside map is changed.
802
818
/// assert_eq!(map.len(), 4);
803
819
/// // But map capacity is equal to old one.
804
- /// assert !(map.capacity() == capacity_before_retain);
820
+ /// assert_eq !(map.capacity(), capacity_before_retain);
805
821
///
806
822
/// let mut vec: Vec<(i32, i32)> = map.iter().map(|(&k, &v)| (k, v)).collect();
807
823
/// vec.sort_unstable();
@@ -825,12 +841,19 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
825
841
/// Drains elements which are true under the given predicate,
826
842
/// and returns an iterator over the removed items.
827
843
///
828
- /// In other words, move all pairs `(k, v)` such that `f(&k,&mut v)` returns `true` out
844
+ /// In other words, move all pairs `(k, v)` such that `f(&k, &mut v)` returns `true` out
829
845
/// into another iterator.
830
846
///
847
+ /// Note that `drain_filter` lets you mutate every value in the filter closure, regardless of
848
+ /// whether you choose to keep or remove it.
849
+ ///
831
850
/// When the returned DrainedFilter is dropped, any remaining elements that satisfy
832
851
/// the predicate are dropped from the table.
833
852
///
853
+ /// It is unspecified how many more elements will be subjected to the closure
854
+ /// if a panic occurs in the closure, or a panic occurs while dropping an element,
855
+ /// or if the `DrainFilter` value is leaked.
856
+ ///
834
857
/// Keeps the allocated memory for reuse.
835
858
///
836
859
/// # Examples
@@ -851,6 +874,16 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
851
874
/// assert_eq!(odds, vec![1, 3, 5, 7]);
852
875
/// // Map capacity is equal to old one.
853
876
/// assert_eq!(map.capacity(), capacity_before_drain_filter);
877
+ ///
878
+ /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x)).collect();
879
+ ///
880
+ /// { // Iterator is dropped without being consumed.
881
+ /// let d = map.drain_filter(|k, _v| k % 2 != 0);
882
+ /// }
883
+ ///
884
+ /// // But the map lens have been reduced by half
885
+ /// // even if we do not use DrainFilter iterator.
886
+ /// assert_eq!(map.len(), 4);
854
887
/// ```
855
888
#[ cfg_attr( feature = "inline-more" , inline) ]
856
889
pub fn drain_filter < F > ( & mut self , f : F ) -> DrainFilter < ' _ , K , V , F , A >
@@ -1020,14 +1053,6 @@ where
1020
1053
/// },
1021
1054
/// _ => panic!(),
1022
1055
/// }
1023
- ///
1024
- /// match map.try_reserve(usize::MAX / 64) {
1025
- /// Err(error) => match error {
1026
- /// TryReserveError::AllocError { .. } => {}
1027
- /// _ => panic!("TryReserveError::CapacityOverflow ?"),
1028
- /// },
1029
- /// _ => panic!(),
1030
- /// }
1031
1056
/// # }
1032
1057
/// # fn main() {
1033
1058
/// # #[cfg(not(miri))]
0 commit comments