From 881062776afe4575f3c9d6534f688a70cf34a7db Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:41:06 -0400 Subject: [PATCH 1/7] Add doc example for HashSet::hasher. --- src/libstd/collections/hash/set.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index d80df5f18b610..040595bbb0426 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -215,6 +215,17 @@ impl HashSet /// Returns a reference to the set's [`BuildHasher`]. /// /// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// use std::collections::hash_map::RandomState; + /// + /// let hasher = RandomState::new(); + /// let set: HashSet = HashSet::with_hasher(hasher); + /// let hasher: &RandomState = set.hasher(); + /// ``` #[stable(feature = "hashmap_public_hasher", since = "1.9.0")] pub fn hasher(&self) -> &S { self.map.hasher() From 9e192602860c897974e81c0c93b9c7293ce0fd3e Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:42:59 -0400 Subject: [PATCH 2/7] Show that the capacity changed in HashSet::reserve doc example. --- src/libstd/collections/hash/set.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 040595bbb0426..0e65a30f3b2a3 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -260,6 +260,7 @@ impl HashSet /// use std::collections::HashSet; /// let mut set: HashSet = HashSet::new(); /// set.reserve(10); + /// assert!(set.capacity() >= 10); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn reserve(&mut self, additional: usize) { From 070eb3c66775789fbfe1607d3d6ef643c9afe3db Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:44:43 -0400 Subject: [PATCH 3/7] Indicate HashSet is code-like in docs. --- 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 0e65a30f3b2a3..ff19a0a1f149d 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -123,7 +123,7 @@ pub struct HashSet { } impl HashSet { - /// Creates an empty HashSet. + /// Creates an empty `HashSet`. /// /// # Examples /// From 9e2b0c6390526e46f7bd217e13c281f7895bd1d9 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:46:41 -0400 Subject: [PATCH 4/7] Remove unnecessary 'mut' bindings. --- src/libstd/collections/hash/set.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index ff19a0a1f149d..f8a3166571403 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -129,7 +129,7 @@ impl HashSet { /// /// ``` /// use std::collections::HashSet; - /// let mut set: HashSet = HashSet::new(); + /// let set: HashSet = HashSet::new(); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] @@ -146,7 +146,7 @@ impl HashSet { /// /// ``` /// use std::collections::HashSet; - /// let mut set: HashSet = HashSet::with_capacity(10); + /// let set: HashSet = HashSet::with_capacity(10); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] From 1599fad5b485cbfbed698df3590665f064999948 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:47:17 -0400 Subject: [PATCH 5/7] Show the capacity in HashSet::with_capacity doc example. --- src/libstd/collections/hash/set.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index f8a3166571403..a02674ed109a6 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -147,6 +147,7 @@ impl HashSet { /// ``` /// use std::collections::HashSet; /// let set: HashSet = HashSet::with_capacity(10); + /// assert!(set.capacity() >= 10); /// ``` #[inline] #[stable(feature = "rust1", since = "1.0.0")] From 34c1bfb0e142587bbbede848b69b2d498b8ede34 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 07:55:44 -0400 Subject: [PATCH 6/7] Remove unnecessary clones in doc examples. --- src/libstd/collections/hash/set.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index a02674ed109a6..3c39db3fbabb3 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -325,13 +325,13 @@ impl HashSet /// println!("{}", x); // Print 1 /// } /// - /// let diff: HashSet<_> = a.difference(&b).cloned().collect(); - /// assert_eq!(diff, [1].iter().cloned().collect()); + /// let diff: HashSet<_> = a.difference(&b).collect(); + /// assert_eq!(diff, [1].iter().collect()); /// /// // Note that difference is not symmetric, /// // and `b - a` means something else: - /// let diff: HashSet<_> = b.difference(&a).cloned().collect(); - /// assert_eq!(diff, [4].iter().cloned().collect()); + /// let diff: HashSet<_> = b.difference(&a).collect(); + /// assert_eq!(diff, [4].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn difference<'a>(&'a self, other: &'a HashSet) -> Difference<'a, T, S> { @@ -356,11 +356,11 @@ impl HashSet /// println!("{}", x); /// } /// - /// let diff1: HashSet<_> = a.symmetric_difference(&b).cloned().collect(); - /// let diff2: HashSet<_> = b.symmetric_difference(&a).cloned().collect(); + /// let diff1: HashSet<_> = a.symmetric_difference(&b).collect(); + /// let diff2: HashSet<_> = b.symmetric_difference(&a).collect(); /// /// assert_eq!(diff1, diff2); - /// assert_eq!(diff1, [1, 4].iter().cloned().collect()); + /// assert_eq!(diff1, [1, 4].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn symmetric_difference<'a>(&'a self, @@ -384,8 +384,8 @@ impl HashSet /// println!("{}", x); /// } /// - /// let intersection: HashSet<_> = a.intersection(&b).cloned().collect(); - /// assert_eq!(intersection, [2, 3].iter().cloned().collect()); + /// let intersection: HashSet<_> = a.intersection(&b).collect(); + /// assert_eq!(intersection, [2, 3].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn intersection<'a>(&'a self, other: &'a HashSet) -> Intersection<'a, T, S> { @@ -410,8 +410,8 @@ impl HashSet /// println!("{}", x); /// } /// - /// let union: HashSet<_> = a.union(&b).cloned().collect(); - /// assert_eq!(union, [1, 2, 3, 4].iter().cloned().collect()); + /// let union: HashSet<_> = a.union(&b).collect(); + /// assert_eq!(union, [1, 2, 3, 4].iter().collect()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn union<'a>(&'a self, other: &'a HashSet) -> Union<'a, T, S> { From d9df2963ad40b67aecde95cbfe98599a45351352 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Tue, 1 Aug 2017 08:12:01 -0400 Subject: [PATCH 7/7] Add doc example for HashSet::drain. --- src/libstd/collections/hash/set.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index 3c39db3fbabb3..80a223c7d74ea 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -453,6 +453,22 @@ impl HashSet } /// Clears the set, returning all elements in an iterator. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// + /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); + /// assert!(!set.is_empty()); + /// + /// // print 1, 2, 3 in an arbitrary order + /// for i in set.drain() { + /// println!("{}", i); + /// } + /// + /// assert!(set.is_empty()); + /// ``` #[inline] #[stable(feature = "drain", since = "1.6.0")] pub fn drain(&mut self) -> Drain {