Rollup merge of #62656 - RalfJung:contains-no-own, r=Dylan-DPC

explain how to search in slice without owned data

Cc https://github.com/rust-lang/rust/issues/62367
This commit is contained in:
Mark Rousskov 2019-07-23 12:51:05 -04:00 committed by GitHub
commit 4264f8376a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1263,6 +1263,15 @@ impl<T> [T] {
/// assert!(v.contains(&30));
/// assert!(!v.contains(&50));
/// ```
///
/// If you do not have an `&T`, but just an `&U` such that `T: Borrow<U>`
/// (e.g. `String: Borrow<str>`), you can use `iter().any`:
///
/// ```
/// 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")]
pub fn contains(&self, x: &T) -> bool
where T: PartialEq