about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorFrank Rehberger <frehberg@gmail.com>2016-09-26 13:39:31 +0200
committerFrank Rehberger <frehberg@gmail.com>2016-09-26 13:39:31 +0200
commitaed99c800e03bcda172ee3b5505ebcab8c5060e9 (patch)
tree1a83f05de16cb5d46869a819b19a3c7df238bab6 /src/libstd
parent95abee1a680f008fb97472294dd376a66e06d311 (diff)
downloadrust-aed99c800e03bcda172ee3b5505ebcab8c5060e9.tar.gz
rust-aed99c800e03bcda172ee3b5505ebcab8c5060e9.zip
Document init of HashSet/HashMap from vector
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/collections/hash/map.rs17
-rw-r--r--src/libstd/collections/hash/set.rs14
2 files changed, 31 insertions, 0 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs
index eb1653f18cb..698ce8815bd 100644
--- a/src/libstd/collections/hash/map.rs
+++ b/src/libstd/collections/hash/map.rs
@@ -335,6 +335,23 @@ fn test_resize_policy() {
 ///     println!("{:?} has {} hp", viking, health);
 /// }
 /// ```
+/// A HashMap with fixed list of elements can be initialized from vector:
+/// ```
+/// use std::collections::HashMap;
+///
+/// fn main() {
+///     let timber_resources: HashMap<&str, i32> =
+///     [ ("Norway", 100),
+///       ("Denmark", 50),
+///       ("Iceland", 10) ]
+///       .iter().map(|&x| x).collect();
+///     // use the values store in map
+/// }
+/// ```
+/// This works for Copy types, if you want to cover non-copy types then you need to replace 
+/// the map(|&x| x) with map(|x| x.clone())
+
+
 #[derive(Clone)]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct HashMap<K, V, S = RandomState> {
diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs
index ff56747fee6..568163b77de 100644
--- a/src/libstd/collections/hash/set.rs
+++ b/src/libstd/collections/hash/set.rs
@@ -100,6 +100,20 @@ const INITIAL_CAPACITY: usize = 32;
 ///     println!("{:?}", x);
 /// }
 /// ```
+/// HashSet with fixed list of elements can be initialized from vector:
+/// ```
+/// use std::collections::HashSet;
+///
+/// fn main() {
+///     let viking_names: HashSet<&str> =
+///         [ "Einar", "Olaf", "Harald" ].iter().map(|&x| x).collect();
+///     // use the values store in the set
+/// }
+/// ```
+/// This works for Copy types, if you want to cover non-copy types then you need to replace 
+/// the map(|&x| x) with map(|x| x.clone())
+
+
 #[derive(Clone)]
 #[stable(feature = "rust1", since = "1.0.0")]
 pub struct HashSet<T, S = RandomState> {