Skip to content

Commit 966ba8d

Browse files
authored
Rollup merge of rust-lang#62656 - RalfJung:contains-no-own, r=Dylan-DPC
explain how to search in slice without owned data Cc rust-lang#62367
2 parents 9785008 + 3f77f2c commit 966ba8d

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/libcore/slice/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,15 @@ impl<T> [T] {
12631263
/// assert!(v.contains(&30));
12641264
/// assert!(!v.contains(&50));
12651265
/// ```
1266+
///
1267+
/// If you do not have an `&T`, but just an `&U` such that `T: Borrow<U>`
1268+
/// (e.g. `String: Borrow<str>`), you can use `iter().any`:
1269+
///
1270+
/// ```
1271+
/// let v = [String::from("hello"), String::from("world")]; // slice of `String`
1272+
/// assert!(v.iter().any(|e| e == "hello")); // search with `&str`
1273+
/// assert!(!v.iter().any(|e| e == "hi"));
1274+
/// ```
12661275
#[stable(feature = "rust1", since = "1.0.0")]
12671276
pub fn contains(&self, x: &T) -> bool
12681277
where T: PartialEq

0 commit comments

Comments
 (0)