Skip to content

Commit

Permalink
Rollup merge of rust-lang#62656 - RalfJung:contains-no-own, r=Dylan-DPC
Browse files Browse the repository at this point in the history
explain how to search in slice without owned data

Cc rust-lang#62367
  • Loading branch information
Centril committed Jul 22, 2019
2 parents 9785008 + 3f77f2c commit 966ba8d
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 966ba8d

Please sign in to comment.