@@ -872,6 +872,52 @@ where
872872 {
873873 self . base . retain ( f)
874874 }
875+
876+ /// Creates a consuming iterator visiting all the keys in arbitrary order.
877+ /// The map cannot be used after calling this.
878+ /// The iterator element type is `K`.
879+ ///
880+ /// # Examples
881+ ///
882+ /// ```
883+ /// #![feature(map_into_keys_values)]
884+ /// use std::collections::HashMap;
885+ ///
886+ /// let mut map = HashMap::new();
887+ /// map.insert("a", 1);
888+ /// map.insert("b", 2);
889+ /// map.insert("c", 3);
890+ ///
891+ /// let vec: Vec<&str> = map.into_keys().collect();
892+ /// ```
893+ #[ inline]
894+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
895+ pub fn into_keys ( self ) -> IntoKeys < K , V > {
896+ IntoKeys { inner : self . into_iter ( ) }
897+ }
898+
899+ /// Creates a consuming iterator visiting all the values in arbitrary order.
900+ /// The map cannot be used after calling this.
901+ /// The iterator element type is `V`.
902+ ///
903+ /// # Examples
904+ ///
905+ /// ```
906+ /// #![feature(map_into_keys_values)]
907+ /// use std::collections::HashMap;
908+ ///
909+ /// let mut map = HashMap::new();
910+ /// map.insert("a", 1);
911+ /// map.insert("b", 2);
912+ /// map.insert("c", 3);
913+ ///
914+ /// let vec: Vec<i32> = map.into_values().collect();
915+ /// ```
916+ #[ inline]
917+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
918+ pub fn into_values ( self ) -> IntoValues < K , V > {
919+ IntoValues { inner : self . into_iter ( ) }
920+ }
875921}
876922
877923impl < K , V , S > HashMap < K , V , S >
@@ -1154,6 +1200,28 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> {
11541200 inner : IterMut < ' a , K , V > ,
11551201}
11561202
1203+ /// An owning iterator over the keys of a `HashMap`.
1204+ ///
1205+ /// This `struct` is created by the [`into_keys`] method on [`HashMap`].
1206+ /// See its documentation for more.
1207+ ///
1208+ /// [`into_keys`]: HashMap::into_keys
1209+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1210+ pub struct IntoKeys < K , V > {
1211+ inner : IntoIter < K , V > ,
1212+ }
1213+
1214+ /// An owning iterator over the values of a `HashMap`.
1215+ ///
1216+ /// This `struct` is created by the [`into_values`] method on [`HashMap`].
1217+ /// See its documentation for more.
1218+ ///
1219+ /// [`into_values`]: HashMap::into_values
1220+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1221+ pub struct IntoValues < K , V > {
1222+ inner : IntoIter < K , V > ,
1223+ }
1224+
11571225/// A builder for computing where in a HashMap a key-value pair would be stored.
11581226///
11591227/// See the [`HashMap::raw_entry_mut`] docs for usage examples.
@@ -1827,6 +1895,66 @@ where
18271895 }
18281896}
18291897
1898+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1899+ impl < K , V > Iterator for IntoKeys < K , V > {
1900+ type Item = K ;
1901+
1902+ #[ inline]
1903+ fn next ( & mut self ) -> Option < K > {
1904+ self . inner . next ( ) . map ( |( k, _) | k)
1905+ }
1906+ #[ inline]
1907+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1908+ self . inner . size_hint ( )
1909+ }
1910+ }
1911+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1912+ impl < K , V > ExactSizeIterator for IntoKeys < K , V > {
1913+ #[ inline]
1914+ fn len ( & self ) -> usize {
1915+ self . inner . len ( )
1916+ }
1917+ }
1918+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1919+ impl < K , V > FusedIterator for IntoKeys < K , V > { }
1920+
1921+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1922+ impl < K : Debug , V : Debug > fmt:: Debug for IntoKeys < K , V > {
1923+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1924+ f. debug_list ( ) . entries ( self . inner . iter ( ) . map ( |( k, _) | k) ) . finish ( )
1925+ }
1926+ }
1927+
1928+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1929+ impl < K , V > Iterator for IntoValues < K , V > {
1930+ type Item = V ;
1931+
1932+ #[ inline]
1933+ fn next ( & mut self ) -> Option < V > {
1934+ self . inner . next ( ) . map ( |( _, v) | v)
1935+ }
1936+ #[ inline]
1937+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1938+ self . inner . size_hint ( )
1939+ }
1940+ }
1941+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1942+ impl < K , V > ExactSizeIterator for IntoValues < K , V > {
1943+ #[ inline]
1944+ fn len ( & self ) -> usize {
1945+ self . inner . len ( )
1946+ }
1947+ }
1948+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1949+ impl < K , V > FusedIterator for IntoValues < K , V > { }
1950+
1951+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1952+ impl < K : Debug , V : Debug > fmt:: Debug for IntoValues < K , V > {
1953+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1954+ f. debug_list ( ) . entries ( self . inner . iter ( ) . map ( |( _, v) | v) ) . finish ( )
1955+ }
1956+ }
1957+
18301958#[ stable( feature = "drain" , since = "1.6.0" ) ]
18311959impl < ' a , K , V > Iterator for Drain < ' a , K , V > {
18321960 type Item = ( K , V ) ;
@@ -3084,6 +3212,30 @@ mod test_map {
30843212 assert ! ( values. contains( & 6 ) ) ;
30853213 }
30863214
3215+ #[ test]
3216+ fn test_into_keys ( ) {
3217+ let vec = vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) ] ;
3218+ let map: HashMap < _ , _ > = vec. into_iter ( ) . collect ( ) ;
3219+ let keys: Vec < _ > = map. into_keys ( ) . collect ( ) ;
3220+
3221+ assert_eq ! ( keys. len( ) , 3 ) ;
3222+ assert ! ( keys. contains( & 1 ) ) ;
3223+ assert ! ( keys. contains( & 2 ) ) ;
3224+ assert ! ( keys. contains( & 3 ) ) ;
3225+ }
3226+
3227+ #[ test]
3228+ fn test_into_values ( ) {
3229+ let vec = vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) ] ;
3230+ let map: HashMap < _ , _ > = vec. into_iter ( ) . collect ( ) ;
3231+ let values: Vec < _ > = map. into_values ( ) . collect ( ) ;
3232+
3233+ assert_eq ! ( values. len( ) , 3 ) ;
3234+ assert ! ( values. contains( & 'a' ) ) ;
3235+ assert ! ( values. contains( & 'b' ) ) ;
3236+ assert ! ( values. contains( & 'c' ) ) ;
3237+ }
3238+
30873239 #[ test]
30883240 fn test_find ( ) {
30893241 let mut m = HashMap :: new ( ) ;
0 commit comments