Skip to content

Commit 4676bb0

Browse files
author
Jonathan Turner
authored
Rollup merge of rust-lang#36740 - frehberg:apidoc, r=steveklabnik
Document init of HashSet/HashMap from vector
2 parents fe0729d + f953d25 commit 4676bb0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,22 @@ fn test_resize_policy() {
335335
/// println!("{:?} has {} hp", viking, health);
336336
/// }
337337
/// ```
338+
///
339+
/// A HashMap with fixed list of elements can be initialized from an array:
340+
///
341+
/// ```
342+
/// use std::collections::HashMap;
343+
///
344+
/// fn main() {
345+
/// let timber_resources: HashMap<&str, i32> =
346+
/// [("Norway", 100),
347+
/// ("Denmark", 50),
348+
/// ("Iceland", 10)]
349+
/// .iter().cloned().collect();
350+
/// // use the values stored in map
351+
/// }
352+
/// ```
353+
338354
#[derive(Clone)]
339355
#[stable(feature = "rust1", since = "1.0.0")]
340356
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
@@ -98,6 +98,20 @@ use super::map::{self, HashMap, Keys, RandomState};
9898
/// println!("{:?}", x);
9999
/// }
100100
/// ```
101+
///
102+
/// HashSet with fixed list of elements can be initialized from an array:
103+
///
104+
/// ```
105+
/// use std::collections::HashSet;
106+
///
107+
/// fn main() {
108+
/// let viking_names: HashSet<&str> =
109+
/// [ "Einar", "Olaf", "Harald" ].iter().cloned().collect();
110+
/// // use the values stored in the set
111+
/// }
112+
/// ```
113+
114+
101115
#[derive(Clone)]
102116
#[stable(feature = "rust1", since = "1.0.0")]
103117
pub struct HashSet<T, S = RandomState> {

0 commit comments

Comments
 (0)