Skip to content

Commit e31116a

Browse files
committed
Add into_{keys,values} methods for HashMap
1 parent 8b26609 commit e31116a

File tree

1 file changed

+128
-0
lines changed
  • library/std/src/collections/hash

1 file changed

+128
-0
lines changed

library/std/src/collections/hash/map.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = "55214")]
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 = "55214")]
918+
pub fn into_values(self) -> IntoValues<K, V> {
919+
IntoValues { inner: self.into_iter() }
920+
}
875921
}
876922

877923
impl<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 = "55214")]
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 = "55214")]
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 = "55214")]
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 = "55214")]
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 = "55214")]
1919+
impl<K, V> FusedIterator for IntoKeys<K, V> {}
1920+
1921+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
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()).finish()
1925+
}
1926+
}
1927+
1928+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
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 = "55214")]
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 = "55214")]
1949+
impl<K, V> FusedIterator for IntoValues<K, V> {}
1950+
1951+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
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()).finish()
1955+
}
1956+
}
1957+
18301958
#[stable(feature = "drain", since = "1.6.0")]
18311959
impl<'a, K, V> Iterator for Drain<'a, K, V> {
18321960
type Item = (K, V);

0 commit comments

Comments
 (0)