@@ -230,6 +230,13 @@ impl<K, V, S> IndexMap<K, V, S> {
230
230
}
231
231
}
232
232
233
+ /// Return an owning iterator over the keys of the map, in their order
234
+ pub fn into_keys ( self ) -> IntoKeys < K , V > {
235
+ IntoKeys {
236
+ iter : self . into_entries ( ) . into_iter ( ) ,
237
+ }
238
+ }
239
+
233
240
/// Return an iterator over the values of the map, in their order
234
241
pub fn values ( & self ) -> Values < ' _ , K , V > {
235
242
Values {
@@ -245,6 +252,13 @@ impl<K, V, S> IndexMap<K, V, S> {
245
252
}
246
253
}
247
254
255
+ /// Return an owning iterator over the values of the map, in their order
256
+ pub fn into_values ( self ) -> IntoValues < K , V > {
257
+ IntoValues {
258
+ iter : self . into_entries ( ) . into_iter ( ) ,
259
+ }
260
+ }
261
+
248
262
/// Remove all key-value pairs in the map, while preserving its capacity.
249
263
///
250
264
/// Computes in **O(n)** time.
@@ -825,6 +839,42 @@ impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> {
825
839
}
826
840
}
827
841
842
+ /// An owning iterator over the keys of a `IndexMap`.
843
+ ///
844
+ /// This `struct` is created by the [`into_keys`] method on [`IndexMap`].
845
+ /// See its documentation for more.
846
+ ///
847
+ /// [`IndexMap`]: struct.IndexMap.html
848
+ /// [`into_keys`]: struct.IndexMap.html#method.into_keys
849
+ pub struct IntoKeys < K , V > {
850
+ iter : vec:: IntoIter < Bucket < K , V > > ,
851
+ }
852
+
853
+ impl < K , V > Iterator for IntoKeys < K , V > {
854
+ type Item = K ;
855
+
856
+ iterator_methods ! ( Bucket :: key) ;
857
+ }
858
+
859
+ impl < K , V > DoubleEndedIterator for IntoKeys < K , V > {
860
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
861
+ self . iter . next_back ( ) . map ( Bucket :: key)
862
+ }
863
+ }
864
+
865
+ impl < K , V > ExactSizeIterator for IntoKeys < K , V > {
866
+ fn len ( & self ) -> usize {
867
+ self . iter . len ( )
868
+ }
869
+ }
870
+
871
+ impl < K : fmt:: Debug , V > fmt:: Debug for IntoKeys < K , V > {
872
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
873
+ let iter = self . iter . as_slice ( ) . iter ( ) . map ( Bucket :: key_ref) ;
874
+ f. debug_list ( ) . entries ( iter) . finish ( )
875
+ }
876
+ }
877
+
828
878
/// An iterator over the values of a `IndexMap`.
829
879
///
830
880
/// This `struct` is created by the [`values`] method on [`IndexMap`]. See its
@@ -898,6 +948,42 @@ impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
898
948
}
899
949
}
900
950
951
+ /// An owning iterator over the values of a `IndexMap`.
952
+ ///
953
+ /// This `struct` is created by the [`into_values`] method on [`IndexMap`].
954
+ /// See its documentation for more.
955
+ ///
956
+ /// [`IndexMap`]: struct.IndexMap.html
957
+ /// [`into_values`]: struct.IndexMap.html#method.into_values
958
+ pub struct IntoValues < K , V > {
959
+ iter : vec:: IntoIter < Bucket < K , V > > ,
960
+ }
961
+
962
+ impl < K , V > Iterator for IntoValues < K , V > {
963
+ type Item = V ;
964
+
965
+ iterator_methods ! ( Bucket :: value) ;
966
+ }
967
+
968
+ impl < K , V > DoubleEndedIterator for IntoValues < K , V > {
969
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
970
+ self . iter . next_back ( ) . map ( Bucket :: value)
971
+ }
972
+ }
973
+
974
+ impl < K , V > ExactSizeIterator for IntoValues < K , V > {
975
+ fn len ( & self ) -> usize {
976
+ self . iter . len ( )
977
+ }
978
+ }
979
+
980
+ impl < K , V : fmt:: Debug > fmt:: Debug for IntoValues < K , V > {
981
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
982
+ let iter = self . iter . as_slice ( ) . iter ( ) . map ( Bucket :: value_ref) ;
983
+ f. debug_list ( ) . entries ( iter) . finish ( )
984
+ }
985
+ }
986
+
901
987
/// An iterator over the entries of a `IndexMap`.
902
988
///
903
989
/// This `struct` is created by the [`iter`] method on [`IndexMap`]. See its
@@ -1683,6 +1769,17 @@ mod tests {
1683
1769
assert ! ( keys. contains( & 3 ) ) ;
1684
1770
}
1685
1771
1772
+ #[ test]
1773
+ fn into_keys ( ) {
1774
+ let vec = vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) ] ;
1775
+ let map: IndexMap < _ , _ > = vec. into_iter ( ) . collect ( ) ;
1776
+ let keys: Vec < i32 > = map. into_keys ( ) . collect ( ) ;
1777
+ assert_eq ! ( keys. len( ) , 3 ) ;
1778
+ assert ! ( keys. contains( & 1 ) ) ;
1779
+ assert ! ( keys. contains( & 2 ) ) ;
1780
+ assert ! ( keys. contains( & 3 ) ) ;
1781
+ }
1782
+
1686
1783
#[ test]
1687
1784
fn values ( ) {
1688
1785
let vec = vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) ] ;
@@ -1707,4 +1804,15 @@ mod tests {
1707
1804
assert ! ( values. contains( & 4 ) ) ;
1708
1805
assert ! ( values. contains( & 6 ) ) ;
1709
1806
}
1807
+
1808
+ #[ test]
1809
+ fn into_values ( ) {
1810
+ let vec = vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) , ( 3 , 'c' ) ] ;
1811
+ let map: IndexMap < _ , _ > = vec. into_iter ( ) . collect ( ) ;
1812
+ let values: Vec < char > = map. into_values ( ) . collect ( ) ;
1813
+ assert_eq ! ( values. len( ) , 3 ) ;
1814
+ assert ! ( values. contains( & 'a' ) ) ;
1815
+ assert ! ( values. contains( & 'b' ) ) ;
1816
+ assert ! ( values. contains( & 'c' ) ) ;
1817
+ }
1710
1818
}
0 commit comments