From 15bc9b6d81a7c9f65d7b8b3224d0d552d051cd7a Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sat, 4 Jul 2020 23:50:01 +0200 Subject: [PATCH 1/3] First version of contains_ref --- library/core/src/slice/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 9ed5a1f962215..09591aa3ff119 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -1407,6 +1407,23 @@ impl [T] { x.slice_contains(self) } + /// Returns `true` if the slice contains an element with the given value. + /// + /// # Examples + /// + /// ``` + /// let v = [String::from("Hello"), String::from("world")]; + /// assert!(v.contains_ref("Hello")); + /// assert!(!v.contains_ref("Rust")); + /// ``` + #[unstable(feature = "contains_ref", issue = "none")] + pub fn contains_ref(&self, x: &U) -> bool + where + T: PartialEq, + { + self.iter().any(|e| e == x) + } + /// Returns `true` if `needle` is a prefix of the slice. /// /// # Examples From 6751769cce9047ba5b1d920f195bae5c4730ff25 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sun, 5 Jul 2020 00:01:47 +0200 Subject: [PATCH 2/3] Fix the bound on U and the feature in the doc test --- library/core/src/slice/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 09591aa3ff119..1a66d98988dec 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -1412,6 +1412,7 @@ impl [T] { /// # Examples /// /// ``` + /// # #![feature(contains_ref)] /// let v = [String::from("Hello"), String::from("world")]; /// assert!(v.contains_ref("Hello")); /// assert!(!v.contains_ref("Rust")); @@ -1420,6 +1421,7 @@ impl [T] { pub fn contains_ref(&self, x: &U) -> bool where T: PartialEq, + U: ?Sized, { self.iter().any(|e| e == x) } From 69637c331bf177c01b9b2333fce43363c14fe983 Mon Sep 17 00:00:00 2001 From: Alexis Bourget Date: Sat, 5 Sep 2020 13:54:59 +0200 Subject: [PATCH 3/3] Expose feature gate in example --- library/core/src/slice/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 1a66d98988dec..a5baf542f017f 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -1412,7 +1412,7 @@ impl [T] { /// # Examples /// /// ``` - /// # #![feature(contains_ref)] + /// #![feature(contains_ref)] /// let v = [String::from("Hello"), String::from("world")]; /// assert!(v.contains_ref("Hello")); /// assert!(!v.contains_ref("Rust"));