From aed99c800e03bcda172ee3b5505ebcab8c5060e9 Mon Sep 17 00:00:00 2001 From: Frank Rehberger Date: Mon, 26 Sep 2016 13:39:31 +0200 Subject: [PATCH 1/7] Document init of HashSet/HashMap from vector --- src/libstd/collections/hash/map.rs | 17 +++++++++++++++++ src/libstd/collections/hash/set.rs | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index eb1653f18cba1..698ce8815bd83 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 { diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index ff56747fee6af..568163b77de8e 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 { From bd80e7bce77aae5a7d515a1113ae3a471d4ce6e2 Mon Sep 17 00:00:00 2001 From: Frank Rehberger Date: Mon, 26 Sep 2016 16:00:24 +0200 Subject: [PATCH 2/7] Update map.rs --- src/libstd/collections/hash/map.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 698ce8815bd83..776643b786632 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -344,13 +344,10 @@ fn test_resize_policy() { /// [ ("Norway", 100), /// ("Denmark", 50), /// ("Iceland", 10) ] -/// .iter().map(|&x| x).collect(); -/// // use the values store in map +/// .iter().cloned().collect(); +/// // use the values stored 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")] From 35510080220fc4f586075dd483ae1bbb67503c44 Mon Sep 17 00:00:00 2001 From: Frank Rehberger Date: Mon, 26 Sep 2016 16:03:37 +0200 Subject: [PATCH 3/7] Update set.rs --- src/libstd/collections/hash/set.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 568163b77de8e..572e333def4da 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -106,12 +106,10 @@ const INITIAL_CAPACITY: usize = 32; /// /// fn main() { /// let viking_names: HashSet<&str> = -/// [ "Einar", "Olaf", "Harald" ].iter().map(|&x| x).collect(); -/// // use the values store in the set +/// [ "Einar", "Olaf", "Harald" ].iter().cloned().collect(); +/// // use the values stored 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)] From ba84d4ff817e724b3a910ec4187cb6418e037b5b Mon Sep 17 00:00:00 2001 From: Frank Rehberger Date: Mon, 26 Sep 2016 18:15:27 +0200 Subject: [PATCH 4/7] Update map.rs --- src/libstd/collections/hash/map.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 776643b786632..b884b60365b8f 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -335,7 +335,7 @@ fn test_resize_policy() { /// println!("{:?} has {} hp", viking, health); /// } /// ``` -/// A HashMap with fixed list of elements can be initialized from vector: +/// A HashMap with fixed list of elements can be initialized from an array: /// ``` /// use std::collections::HashMap; /// From 6c4616c72d8f845d2a0555b8f1610c4bf5ca7525 Mon Sep 17 00:00:00 2001 From: Frank Rehberger Date: Mon, 26 Sep 2016 18:16:06 +0200 Subject: [PATCH 5/7] Update set.rs --- src/libstd/collections/hash/set.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 572e333def4da..505bbb33b2053 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -100,7 +100,7 @@ const INITIAL_CAPACITY: usize = 32; /// println!("{:?}", x); /// } /// ``` -/// HashSet with fixed list of elements can be initialized from vector: +/// HashSet with fixed list of elements can be initialized from an array: /// ``` /// use std::collections::HashSet; /// From 81c47d591a90cea7c374cfd893eca2cc67aac284 Mon Sep 17 00:00:00 2001 From: Frank Rehberger Date: Tue, 27 Sep 2016 10:31:25 +0200 Subject: [PATCH 6/7] Update map.rs --- src/libstd/collections/hash/map.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index b884b60365b8f..044d5e4ed3659 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -335,16 +335,18 @@ fn test_resize_policy() { /// println!("{:?} has {} hp", viking, health); /// } /// ``` +/// /// A HashMap with fixed list of elements can be initialized from an array: +/// /// ``` /// use std::collections::HashMap; /// /// fn main() { /// let timber_resources: HashMap<&str, i32> = -/// [ ("Norway", 100), -/// ("Denmark", 50), -/// ("Iceland", 10) ] -/// .iter().cloned().collect(); +/// [("Norway", 100), +/// ("Denmark", 50), +/// ("Iceland", 10)] +/// .iter().cloned().collect(); /// // use the values stored in map /// } /// ``` From f953d2564e2ebb79a20809c7cff85ccbcfc77d14 Mon Sep 17 00:00:00 2001 From: Frank Rehberger Date: Tue, 27 Sep 2016 10:32:21 +0200 Subject: [PATCH 7/7] Update set.rs --- src/libstd/collections/hash/set.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 505bbb33b2053..5a88367e8f483 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -100,7 +100,9 @@ const INITIAL_CAPACITY: usize = 32; /// println!("{:?}", x); /// } /// ``` +/// /// HashSet with fixed list of elements can be initialized from an array: +/// /// ``` /// use std::collections::HashSet; ///