Skip to content

Commit aed99c8

Browse files
committed
Document init of HashSet/HashMap from vector
1 parent 95abee1 commit aed99c8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,23 @@ fn test_resize_policy() {
335335
/// println!("{:?} has {} hp", viking, health);
336336
/// }
337337
/// ```
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+
338355
#[derive(Clone)]
339356
#[stable(feature = "rust1", since = "1.0.0")]
340357
pub struct HashMap<K, V, S = RandomState> {

src/libstd/collections/hash/set.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ const INITIAL_CAPACITY: usize = 32;
100100
/// println!("{:?}", x);
101101
/// }
102102
/// ```
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+
103117
#[derive(Clone)]
104118
#[stable(feature = "rust1", since = "1.0.0")]
105119
pub struct HashSet<T, S = RandomState> {

0 commit comments

Comments
 (0)