@@ -124,8 +124,21 @@ use crate::sys;
124124/// }
125125/// ```
126126///
127- /// `HashMap` also implements an [`Entry API`](#method.entry), which allows
128- /// for more complex methods of getting, setting, updating and removing keys and
127+ /// A `HashMap` with a known list of items can be initialized from an array:
128+ ///
129+ /// ```
130+ /// use std::collections::HashMap;
131+ ///
132+ /// let solar_distance = HashMap::from([
133+ /// ("Mercury", 0.4),
134+ /// ("Venus", 0.7),
135+ /// ("Earth", 1.0),
136+ /// ("Mars", 1.5),
137+ /// ]);
138+ /// ```
139+ ///
140+ /// `HashMap` implements an [`Entry API`](#method.entry), which allows
141+ /// for complex methods of getting, setting, updating and removing keys and
129142/// their values:
130143///
131144/// ```
@@ -179,27 +192,17 @@ use crate::sys;
179192/// }
180193///
181194/// // Use a HashMap to store the vikings' health points.
182- /// let mut vikings = HashMap::new();
183- ///
184- /// vikings.insert (Viking::new("Einar ", "Norway "), 25);
185- /// vikings.insert (Viking::new("Olaf ", "Denmark "), 24);
186- /// vikings.insert(Viking::new("Harald", "Iceland"), 12 );
195+ /// let vikings = HashMap::from([
196+ /// (Viking::new("Einar", "Norway"), 25),
197+ /// (Viking::new("Olaf ", "Denmark "), 24),
198+ /// (Viking::new("Harald ", "Iceland "), 12),
199+ /// ] );
187200///
188201/// // Use derived implementation to print the status of the vikings.
189202/// for (viking, health) in &vikings {
190203/// println!("{:?} has {} hp", viking, health);
191204/// }
192205/// ```
193- ///
194- /// A `HashMap` with fixed list of elements can be initialized from an array:
195- ///
196- /// ```
197- /// use std::collections::HashMap;
198- ///
199- /// let timber_resources: HashMap<&str, i32> = [("Norway", 100), ("Denmark", 50), ("Iceland", 10)]
200- /// .iter().cloned().collect();
201- /// // use the values stored in map
202- /// ```
203206
204207#[ cfg_attr( not( test) , rustc_diagnostic_item = "hashmap_type" ) ]
205208#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1151,6 +1154,37 @@ where
11511154 }
11521155}
11531156
1157+ #[ stable( feature = "std_collections_from_array" , since = "1.55.0" ) ]
1158+ // Note: as what is currently the most convenient built-in way to construct
1159+ // a HashMap, a simple usage of this function must not *require* the user
1160+ // to provide a type annotation in order to infer the third type parameter
1161+ // (the hasher parameter, conventionally "S").
1162+ // To that end, this impl is defined using RandomState as the concrete
1163+ // type of S, rather than being generic over `S: BuildHasher + Default`.
1164+ // It is expected that users who want to specify a hasher will manually use
1165+ // `with_capacity_and_hasher`.
1166+ // If type parameter defaults worked on impls, and if type parameter
1167+ // defaults could be mixed with const generics, then perhaps
1168+ // this could be generalized.
1169+ // See also the equivalent impl on HashSet.
1170+ impl < K , V , const N : usize > From < [ ( K , V ) ; N ] > for HashMap < K , V , RandomState >
1171+ where
1172+ K : Eq + Hash ,
1173+ {
1174+ /// # Examples
1175+ ///
1176+ /// ```
1177+ /// use std::collections::HashMap;
1178+ ///
1179+ /// let map1 = HashMap::from([(1, 2), (3, 4)]);
1180+ /// let map2: HashMap<_, _> = [(1, 2), (3, 4)].into();
1181+ /// assert_eq!(map1, map2);
1182+ /// ```
1183+ fn from ( arr : [ ( K , V ) ; N ] ) -> Self {
1184+ crate :: array:: IntoIter :: new ( arr) . collect ( )
1185+ }
1186+ }
1187+
11541188/// An iterator over the entries of a `HashMap`.
11551189///
11561190/// This `struct` is created by the [`iter`] method on [`HashMap`]. See its
0 commit comments