Skip to content

Commit 15bc9b6

Browse files
committed
First version of contains_ref
1 parent 2c28244 commit 15bc9b6

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

library/core/src/slice/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,23 @@ impl<T> [T] {
14071407
x.slice_contains(self)
14081408
}
14091409

1410+
/// Returns `true` if the slice contains an element with the given value.
1411+
///
1412+
/// # Examples
1413+
///
1414+
/// ```
1415+
/// let v = [String::from("Hello"), String::from("world")];
1416+
/// assert!(v.contains_ref("Hello"));
1417+
/// assert!(!v.contains_ref("Rust"));
1418+
/// ```
1419+
#[unstable(feature = "contains_ref", issue = "none")]
1420+
pub fn contains_ref<U>(&self, x: &U) -> bool
1421+
where
1422+
T: PartialEq<U>,
1423+
{
1424+
self.iter().any(|e| e == x)
1425+
}
1426+
14101427
/// Returns `true` if `needle` is a prefix of the slice.
14111428
///
14121429
/// # Examples

0 commit comments

Comments
 (0)