diff options
| author | Nazım Can Altınova <canaltinova@gmail.com> | 2020-08-04 22:00:32 +0200 |
|---|---|---|
| committer | Nazım Can Altınova <canaltinova@gmail.com> | 2020-08-07 13:13:37 +0200 |
| commit | e31116af50f35ff81f83561ba607c610f42bbf4a (patch) | |
| tree | 70ae8facde07a5156cfa09dcf7384a1a64a0b277 | |
| parent | 8b26609481c956a666f9189738f1ba611078e1ab (diff) | |
| download | rust-e31116af50f35ff81f83561ba607c610f42bbf4a.tar.gz rust-e31116af50f35ff81f83561ba607c610f42bbf4a.zip | |
Add `into_{keys,values}` methods for HashMap
| -rw-r--r-- | library/std/src/collections/hash/map.rs | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index c40d6119fdf..f12009e4c20 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -872,6 +872,52 @@ where { self.base.retain(f) } + + /// Creates a consuming iterator visiting all the keys in arbitrary order. + /// The map cannot be used after calling this. + /// The iterator element type is `K`. + /// + /// # Examples + /// + /// ``` + /// #![feature(map_into_keys_values)] + /// use std::collections::HashMap; + /// + /// let mut map = HashMap::new(); + /// map.insert("a", 1); + /// map.insert("b", 2); + /// map.insert("c", 3); + /// + /// let vec: Vec<&str> = map.into_keys().collect(); + /// ``` + #[inline] + #[unstable(feature = "map_into_keys_values", issue = "55214")] + pub fn into_keys(self) -> IntoKeys<K, V> { + IntoKeys { inner: self.into_iter() } + } + + /// Creates a consuming iterator visiting all the values in arbitrary order. + /// The map cannot be used after calling this. + /// The iterator element type is `V`. + /// + /// # Examples + /// + /// ``` + /// #![feature(map_into_keys_values)] + /// use std::collections::HashMap; + /// + /// let mut map = HashMap::new(); + /// map.insert("a", 1); + /// map.insert("b", 2); + /// map.insert("c", 3); + /// + /// let vec: Vec<i32> = map.into_values().collect(); + /// ``` + #[inline] + #[unstable(feature = "map_into_keys_values", issue = "55214")] + pub fn into_values(self) -> IntoValues<K, V> { + IntoValues { inner: self.into_iter() } + } } impl<K, V, S> HashMap<K, V, S> @@ -1154,6 +1200,28 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> { inner: IterMut<'a, K, V>, } +/// An owning iterator over the keys of a `HashMap`. +/// +/// This `struct` is created by the [`into_keys`] method on [`HashMap`]. +/// See its documentation for more. +/// +/// [`into_keys`]: HashMap::into_keys +#[unstable(feature = "map_into_keys_values", issue = "55214")] +pub struct IntoKeys<K, V> { + inner: IntoIter<K, V>, +} + +/// An owning iterator over the values of a `HashMap`. +/// +/// This `struct` is created by the [`into_values`] method on [`HashMap`]. +/// See its documentation for more. +/// +/// [`into_values`]: HashMap::into_values +#[unstable(feature = "map_into_keys_values", issue = "55214")] +pub struct IntoValues<K, V> { + inner: IntoIter<K, V>, +} + /// A builder for computing where in a HashMap a key-value pair would be stored. /// /// See the [`HashMap::raw_entry_mut`] docs for usage examples. @@ -1827,6 +1895,66 @@ where } } +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> Iterator for IntoKeys<K, V> { + type Item = K; + + #[inline] + fn next(&mut self) -> Option<K> { + self.inner.next().map(|(k, _)| k) + } + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + self.inner.size_hint() + } +} +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> ExactSizeIterator for IntoKeys<K, V> { + #[inline] + fn len(&self) -> usize { + self.inner.len() + } +} +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> FusedIterator for IntoKeys<K, V> {} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K: Debug, V: Debug> fmt::Debug for IntoKeys<K, V> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list().entries(self.inner.iter()).finish() + } +} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> Iterator for IntoValues<K, V> { + type Item = V; + + #[inline] + fn next(&mut self) -> Option<V> { + self.inner.next().map(|(_, v)| v) + } + #[inline] + fn size_hint(&self) -> (usize, Option<usize>) { + self.inner.size_hint() + } +} +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> ExactSizeIterator for IntoValues<K, V> { + #[inline] + fn len(&self) -> usize { + self.inner.len() + } +} +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> FusedIterator for IntoValues<K, V> {} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K: Debug, V: Debug> fmt::Debug for IntoValues<K, V> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_list().entries(self.inner.iter()).finish() + } +} + #[stable(feature = "drain", since = "1.6.0")] impl<'a, K, V> Iterator for Drain<'a, K, V> { type Item = (K, V); |
