File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed
src/libstd/collections/hash Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -335,6 +335,23 @@ fn test_resize_policy() {
335
335
/// println!("{:?} has {} hp", viking, health);
336
336
/// }
337
337
/// ```
338
+ /// A HashMap with fixed list of elements can be initialized from vector:
339
+ /// ```
340
+ /// use std::collections::HashMap;
341
+ ///
342
+ /// fn main() {
343
+ /// let timber_resources: HashMap<&str, i32> =
344
+ /// [ ("Norway", 100),
345
+ /// ("Denmark", 50),
346
+ /// ("Iceland", 10) ]
347
+ /// .iter().map(|&x| x).collect();
348
+ /// // use the values store in map
349
+ /// }
350
+ /// ```
351
+ /// This works for Copy types, if you want to cover non-copy types then you need to replace
352
+ /// the map(|&x| x) with map(|x| x.clone())
353
+
354
+
338
355
#[ derive( Clone ) ]
339
356
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
340
357
pub struct HashMap < K , V , S = RandomState > {
Original file line number Diff line number Diff line change @@ -100,6 +100,20 @@ const INITIAL_CAPACITY: usize = 32;
100
100
/// println!("{:?}", x);
101
101
/// }
102
102
/// ```
103
+ /// HashSet with fixed list of elements can be initialized from vector:
104
+ /// ```
105
+ /// use std::collections::HashSet;
106
+ ///
107
+ /// fn main() {
108
+ /// let viking_names: HashSet<&str> =
109
+ /// [ "Einar", "Olaf", "Harald" ].iter().map(|&x| x).collect();
110
+ /// // use the values store in the set
111
+ /// }
112
+ /// ```
113
+ /// This works for Copy types, if you want to cover non-copy types then you need to replace
114
+ /// the map(|&x| x) with map(|x| x.clone())
115
+
116
+
103
117
#[ derive( Clone ) ]
104
118
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
105
119
pub struct HashSet < T , S = RandomState > {
You can’t perform that action at this time.
0 commit comments