@@ -349,6 +349,33 @@ impl<K, V, S> HashMap<K, V, S> {
349349 Keys { inner : self . iter ( ) }
350350 }
351351
352+ /// Creates a consuming iterator visiting all the keys in arbitrary order.
353+ /// The map cannot be used after calling this.
354+ /// The iterator element type is `K`.
355+ ///
356+ /// # Examples
357+ ///
358+ /// ```
359+ /// use std::collections::HashMap;
360+ ///
361+ /// let map = HashMap::from([
362+ /// ("a", 1),
363+ /// ("b", 2),
364+ /// ("c", 3),
365+ /// ]);
366+ ///
367+ /// let mut vec: Vec<&str> = map.into_keys().collect();
368+ /// // The `IntoKeys` iterator produces keys in arbitrary order, so the
369+ /// // keys must be sorted to test them against a sorted array.
370+ /// vec.sort_unstable();
371+ /// assert_eq!(vec, ["a", "b", "c"]);
372+ /// ```
373+ #[ inline]
374+ #[ stable( feature = "map_into_keys_values" , since = "1.54.0" ) ]
375+ pub fn into_keys ( self ) -> IntoKeys < K , V > {
376+ IntoKeys { inner : self . into_iter ( ) }
377+ }
378+
352379 /// An iterator visiting all values in arbitrary order.
353380 /// The iterator element type is `&'a V`.
354381 ///
@@ -399,6 +426,33 @@ impl<K, V, S> HashMap<K, V, S> {
399426 ValuesMut { inner : self . iter_mut ( ) }
400427 }
401428
429+ /// Creates a consuming iterator visiting all the values in arbitrary order.
430+ /// The map cannot be used after calling this.
431+ /// The iterator element type is `V`.
432+ ///
433+ /// # Examples
434+ ///
435+ /// ```
436+ /// use std::collections::HashMap;
437+ ///
438+ /// let map = HashMap::from([
439+ /// ("a", 1),
440+ /// ("b", 2),
441+ /// ("c", 3),
442+ /// ]);
443+ ///
444+ /// let mut vec: Vec<i32> = map.into_values().collect();
445+ /// // The `IntoValues` iterator produces values in arbitrary order, so
446+ /// // the values must be sorted to test them against a sorted array.
447+ /// vec.sort_unstable();
448+ /// assert_eq!(vec, [1, 2, 3]);
449+ /// ```
450+ #[ inline]
451+ #[ stable( feature = "map_into_keys_values" , since = "1.54.0" ) ]
452+ pub fn into_values ( self ) -> IntoValues < K , V > {
453+ IntoValues { inner : self . into_iter ( ) }
454+ }
455+
402456 /// An iterator visiting all key-value pairs in arbitrary order.
403457 /// The iterator element type is `(&'a K, &'a V)`.
404458 ///
@@ -555,6 +609,29 @@ impl<K, V, S> HashMap<K, V, S> {
555609 DrainFilter { base : self . base . drain_filter ( pred) }
556610 }
557611
612+ /// Retains only the elements specified by the predicate.
613+ ///
614+ /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`.
615+ /// The elements are visited in unsorted (and unspecified) order.
616+ ///
617+ /// # Examples
618+ ///
619+ /// ```
620+ /// use std::collections::HashMap;
621+ ///
622+ /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
623+ /// map.retain(|&k, _| k % 2 == 0);
624+ /// assert_eq!(map.len(), 4);
625+ /// ```
626+ #[ inline]
627+ #[ stable( feature = "retain_hash_collection" , since = "1.18.0" ) ]
628+ pub fn retain < F > ( & mut self , f : F )
629+ where
630+ F : FnMut ( & K , & mut V ) -> bool ,
631+ {
632+ self . base . retain ( f)
633+ }
634+
558635 /// Clears the map, removing all key-value pairs. Keeps the allocated memory
559636 /// for reuse.
560637 ///
@@ -937,83 +1014,6 @@ where
9371014 {
9381015 self . base . remove_entry ( k)
9391016 }
940-
941- /// Retains only the elements specified by the predicate.
942- ///
943- /// In other words, remove all pairs `(k, v)` such that `f(&k, &mut v)` returns `false`.
944- /// The elements are visited in unsorted (and unspecified) order.
945- ///
946- /// # Examples
947- ///
948- /// ```
949- /// use std::collections::HashMap;
950- ///
951- /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
952- /// map.retain(|&k, _| k % 2 == 0);
953- /// assert_eq!(map.len(), 4);
954- /// ```
955- #[ inline]
956- #[ stable( feature = "retain_hash_collection" , since = "1.18.0" ) ]
957- pub fn retain < F > ( & mut self , f : F )
958- where
959- F : FnMut ( & K , & mut V ) -> bool ,
960- {
961- self . base . retain ( f)
962- }
963-
964- /// Creates a consuming iterator visiting all the keys in arbitrary order.
965- /// The map cannot be used after calling this.
966- /// The iterator element type is `K`.
967- ///
968- /// # Examples
969- ///
970- /// ```
971- /// use std::collections::HashMap;
972- ///
973- /// let map = HashMap::from([
974- /// ("a", 1),
975- /// ("b", 2),
976- /// ("c", 3),
977- /// ]);
978- ///
979- /// let mut vec: Vec<&str> = map.into_keys().collect();
980- /// // The `IntoKeys` iterator produces keys in arbitrary order, so the
981- /// // keys must be sorted to test them against a sorted array.
982- /// vec.sort_unstable();
983- /// assert_eq!(vec, ["a", "b", "c"]);
984- /// ```
985- #[ inline]
986- #[ stable( feature = "map_into_keys_values" , since = "1.54.0" ) ]
987- pub fn into_keys ( self ) -> IntoKeys < K , V > {
988- IntoKeys { inner : self . into_iter ( ) }
989- }
990-
991- /// Creates a consuming iterator visiting all the values in arbitrary order.
992- /// The map cannot be used after calling this.
993- /// The iterator element type is `V`.
994- ///
995- /// # Examples
996- ///
997- /// ```
998- /// use std::collections::HashMap;
999- ///
1000- /// let map = HashMap::from([
1001- /// ("a", 1),
1002- /// ("b", 2),
1003- /// ("c", 3),
1004- /// ]);
1005- ///
1006- /// let mut vec: Vec<i32> = map.into_values().collect();
1007- /// // The `IntoValues` iterator produces values in arbitrary order, so
1008- /// // the values must be sorted to test them against a sorted array.
1009- /// vec.sort_unstable();
1010- /// assert_eq!(vec, [1, 2, 3]);
1011- /// ```
1012- #[ inline]
1013- #[ stable( feature = "map_into_keys_values" , since = "1.54.0" ) ]
1014- pub fn into_values ( self ) -> IntoValues < K , V > {
1015- IntoValues { inner : self . into_iter ( ) }
1016- }
10171017}
10181018
10191019impl < K , V , S > HashMap < K , V , S >
0 commit comments