From 80a308df322d426a5f98b0fe4d888cc701a854c6 Mon Sep 17 00:00:00 2001 From: Joseph T Lyons Date: Thu, 2 Dec 2021 12:26:47 -0500 Subject: Use `HashMap::from()` instead of using `HashMap::new()` with `HashMap::insert()` --- library/std/src/collections/hash/map.rs | 123 ++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 53 deletions(-) (limited to 'library/std/src') diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index 12246b5173d..c585827b9eb 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -334,10 +334,11 @@ impl HashMap { /// ``` /// use std::collections::HashMap; /// - /// let mut map = HashMap::new(); - /// map.insert("a", 1); - /// map.insert("b", 2); - /// map.insert("c", 3); + /// let map = HashMap::from([ + /// ("a", 1), + /// ("b", 2), + /// ("c", 3), + /// ]); /// /// for key in map.keys() { /// println!("{}", key); @@ -356,10 +357,11 @@ impl HashMap { /// ``` /// use std::collections::HashMap; /// - /// let mut map = HashMap::new(); - /// map.insert("a", 1); - /// map.insert("b", 2); - /// map.insert("c", 3); + /// let map = HashMap::from([ + /// ("a", 1), + /// ("b", 2), + /// ("c", 3), + /// ]); /// /// for val in map.values() { /// println!("{}", val); @@ -378,11 +380,11 @@ impl HashMap { /// ``` /// use std::collections::HashMap; /// - /// let mut map = HashMap::new(); - /// - /// map.insert("a", 1); - /// map.insert("b", 2); - /// map.insert("c", 3); + /// let map = HashMap::from([ + /// ("a", 1), + /// ("b", 2), + /// ("c", 3), + /// ]); /// /// for val in map.values_mut() { /// *val = *val + 10; @@ -405,10 +407,11 @@ impl HashMap { /// ``` /// use std::collections::HashMap; /// - /// let mut map = HashMap::new(); - /// map.insert("a", 1); - /// map.insert("b", 2); - /// map.insert("c", 3); + /// let map = HashMap::from([ + /// ("a", 1), + /// ("b", 2), + /// ("c", 3), + /// ]); /// /// for (key, val) in map.iter() { /// println!("key: {} val: {}", key, val); @@ -428,10 +431,11 @@ impl HashMap { /// ``` /// use std::collections::HashMap; /// - /// let mut map = HashMap::new(); - /// map.insert("a", 1); - /// map.insert("b", 2); - /// map.insert("c", 3); + /// let map = HashMap::from([ + /// ("a", 1), + /// ("b", 2), + /// ("c", 3), + /// ]); /// /// // Update all values /// for (_, val) in map.iter_mut() { @@ -966,10 +970,11 @@ where /// ``` /// use std::collections::HashMap; /// - /// let mut map = HashMap::new(); - /// map.insert("a", 1); - /// map.insert("b", 2); - /// map.insert("c", 3); + /// let map = HashMap::from([ + /// ("a", 1), + /// ("b", 2), + /// ("c", 3), + /// ]); /// /// let mut vec: Vec<&str> = map.into_keys().collect(); /// // The `IntoKeys` iterator produces keys in arbitrary order, so the @@ -992,10 +997,11 @@ where /// ``` /// use std::collections::HashMap; /// - /// let mut map = HashMap::new(); - /// map.insert("a", 1); - /// map.insert("b", 2); - /// map.insert("c", 3); + /// let map = HashMap::from([ + /// ("a", 1), + /// ("b", 2), + /// ("c", 3), + /// ]); /// /// let mut vec: Vec = map.into_values().collect(); /// // The `IntoValues` iterator produces values in arbitrary order, so @@ -1202,8 +1208,9 @@ where /// ``` /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter = map.iter(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] @@ -1239,8 +1246,9 @@ impl fmt::Debug for Iter<'_, K, V> { /// ``` /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter = map.iter_mut(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] @@ -1269,8 +1277,9 @@ impl<'a, K, V> IterMut<'a, K, V> { /// ``` /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter = map.into_iter(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] @@ -1298,8 +1307,9 @@ impl IntoIter { /// ``` /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter_keys = map.keys(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] @@ -1335,8 +1345,9 @@ impl fmt::Debug for Keys<'_, K, V> { /// ``` /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter_values = map.values(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] @@ -1372,8 +1383,9 @@ impl fmt::Debug for Values<'_, K, V> { /// ``` /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter = map.drain(); /// ``` #[stable(feature = "drain", since = "1.6.0")] @@ -1402,8 +1414,9 @@ impl<'a, K, V> Drain<'a, K, V> { /// /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter = map.drain_filter(|_k, v| *v % 2 == 0); /// ``` #[unstable(feature = "hash_drain_filter", issue = "59618")] @@ -1426,8 +1439,9 @@ where /// ``` /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter_values = map.values_mut(); /// ``` #[stable(feature = "map_values_mut", since = "1.10.0")] @@ -1447,8 +1461,9 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> { /// ``` /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter_keys = map.into_keys(); /// ``` #[stable(feature = "map_into_keys_values", since = "1.54.0")] @@ -1468,8 +1483,9 @@ pub struct IntoKeys { /// ``` /// use std::collections::HashMap; /// -/// let mut map = HashMap::new(); -/// map.insert("a", 1); +/// let map = HashMap::from([ +/// ("a", 1), +/// ]); /// let iter_keys = map.into_values(); /// ``` #[stable(feature = "map_into_keys_values", since = "1.54.0")] @@ -2004,10 +2020,11 @@ impl IntoIterator for HashMap { /// ``` /// use std::collections::HashMap; /// - /// let mut map = HashMap::new(); - /// map.insert("a", 1); - /// map.insert("b", 2); - /// map.insert("c", 3); + /// let map = HashMap::from([ + /// ("a", 1), + /// ("b", 2), + /// ("c", 3), + /// ]); /// /// // Not possible with .iter() /// let vec: Vec<(&str, i32)> = map.into_iter().collect(); -- cgit 1.4.1-3-g733a5 From 72a6974e455793f9dcc370cc8bdf9c055b843d39 Mon Sep 17 00:00:00 2001 From: Joseph T Lyons Date: Fri, 3 Dec 2021 00:14:55 -0500 Subject: Make `HashMap`s mutable again --- library/std/src/collections/hash/map.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'library/std/src') diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index c585827b9eb..fa597cfba6c 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -380,7 +380,7 @@ impl HashMap { /// ``` /// use std::collections::HashMap; /// - /// let map = HashMap::from([ + /// let mut map = HashMap::from([ /// ("a", 1), /// ("b", 2), /// ("c", 3), @@ -431,7 +431,7 @@ impl HashMap { /// ``` /// use std::collections::HashMap; /// - /// let map = HashMap::from([ + /// let mut map = HashMap::from([ /// ("a", 1), /// ("b", 2), /// ("c", 3), @@ -1246,7 +1246,7 @@ impl fmt::Debug for Iter<'_, K, V> { /// ``` /// use std::collections::HashMap; /// -/// let map = HashMap::from([ +/// let mut map = HashMap::from([ /// ("a", 1), /// ]); /// let iter = map.iter_mut(); @@ -1383,7 +1383,7 @@ impl fmt::Debug for Values<'_, K, V> { /// ``` /// use std::collections::HashMap; /// -/// let map = HashMap::from([ +/// let mut map = HashMap::from([ /// ("a", 1), /// ]); /// let iter = map.drain(); @@ -1414,7 +1414,7 @@ impl<'a, K, V> Drain<'a, K, V> { /// /// use std::collections::HashMap; /// -/// let map = HashMap::from([ +/// let mut map = HashMap::from([ /// ("a", 1), /// ]); /// let iter = map.drain_filter(|_k, v| *v % 2 == 0); @@ -1439,7 +1439,7 @@ where /// ``` /// use std::collections::HashMap; /// -/// let map = HashMap::from([ +/// let mut map = HashMap::from([ /// ("a", 1), /// ]); /// let iter_values = map.values_mut(); -- cgit 1.4.1-3-g733a5