From d73c23d114d109d7c48c9447f53bcba533effa16 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 13 Jul 2019 15:16:05 +0200 Subject: [PATCH 1/2] explain how to search without owned data --- src/libcore/slice/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 363ae08827558..c3eb2ec9dc251 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -1263,6 +1263,14 @@ impl [T] { /// assert!(v.contains(&30)); /// assert!(!v.contains(&50)); /// ``` + /// + /// If you only have a borrowed `T`, use `any`: + /// + /// ``` + /// let v = [String::from("hello"), String::from("world")]; + /// assert!(v.iter().any(|e| e == "hello")); + /// assert!(!v.iter().any(|e| e == "hi")); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn contains(&self, x: &T) -> bool where T: PartialEq From 3f77f2cd5bedf364d4226df139e7369761bd41a2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 14 Jul 2019 10:03:04 +0200 Subject: [PATCH 2/2] better comments --- src/libcore/slice/mod.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index c3eb2ec9dc251..edd00a9fa8561 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -1264,11 +1264,12 @@ impl [T] { /// assert!(!v.contains(&50)); /// ``` /// - /// If you only have a borrowed `T`, use `any`: + /// If you do not have an `&T`, but just an `&U` such that `T: Borrow` + /// (e.g. `String: Borrow`), you can use `iter().any`: /// /// ``` - /// let v = [String::from("hello"), String::from("world")]; - /// assert!(v.iter().any(|e| e == "hello")); + /// let v = [String::from("hello"), String::from("world")]; // slice of `String` + /// assert!(v.iter().any(|e| e == "hello")); // search with `&str` /// assert!(!v.iter().any(|e| e == "hi")); /// ``` #[stable(feature = "rust1", since = "1.0.0")]