Skip to content

Commit 6da3098

Browse files
authored
Rollup merge of rust-lang#62356 - soc:topic/contains, r=Centril
Implement Option::contains and Result::contains This increases consistency with other common data structures.
2 parents 6e310f2 + 6f76da4 commit 6da3098

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/libcore/option.rs

+26
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,32 @@ impl<T> Option<T> {
208208
!self.is_some()
209209
}
210210

211+
/// Returns `true` if the option is a [`Some`] value containing the given value.
212+
///
213+
/// # Examples
214+
///
215+
/// ```
216+
/// #![feature(option_result_contains)]
217+
///
218+
/// let x: Option<u32> = Some(2);
219+
/// assert_eq!(x.contains(&2), true);
220+
///
221+
/// let x: Option<u32> = Some(3);
222+
/// assert_eq!(x.contains(&2), false);
223+
///
224+
/// let x: Option<u32> = None;
225+
/// assert_eq!(x.contains(&2), false);
226+
/// ```
227+
#[must_use]
228+
#[inline]
229+
#[unstable(feature = "option_result_contains", issue = "62358")]
230+
pub fn contains<U>(&self, x: &U) -> bool where U: PartialEq<T> {
231+
match self {
232+
Some(y) => x == y,
233+
None => false,
234+
}
235+
}
236+
211237
/////////////////////////////////////////////////////////////////////////
212238
// Adapter for working with references
213239
/////////////////////////////////////////////////////////////////////////

src/libcore/result.rs

+52
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,58 @@ impl<T, E> Result<T, E> {
309309
!self.is_ok()
310310
}
311311

312+
/// Returns `true` if the result is an [`Ok`] value containing the given value.
313+
///
314+
/// # Examples
315+
///
316+
/// ```
317+
/// #![feature(option_result_contains)]
318+
///
319+
/// let x: Result<u32, &str> = Ok(2);
320+
/// assert_eq!(x.contains(&2), true);
321+
///
322+
/// let x: Result<u32, &str> = Ok(3);
323+
/// assert_eq!(x.contains(&2), false);
324+
///
325+
/// let x: Result<u32, &str> = Err("Some error message");
326+
/// assert_eq!(x.contains(&2), false);
327+
/// ```
328+
#[must_use]
329+
#[inline]
330+
#[unstable(feature = "option_result_contains", issue = "62358")]
331+
pub fn contains<U>(&self, x: &U) -> bool where U: PartialEq<T> {
332+
match self {
333+
Ok(y) => x == y,
334+
Err(_) => false
335+
}
336+
}
337+
338+
/// Returns `true` if the result is an [`Err`] value containing the given value.
339+
///
340+
/// # Examples
341+
///
342+
/// ```
343+
/// #![feature(result_contains_err)]
344+
///
345+
/// let x: Result<u32, &str> = Ok(2);
346+
/// assert_eq!(x.contains_err(&"Some error message"), false);
347+
///
348+
/// let x: Result<u32, &str> = Err("Some error message");
349+
/// assert_eq!(x.contains_err(&"Some error message"), true);
350+
///
351+
/// let x: Result<u32, &str> = Err("Some other error message");
352+
/// assert_eq!(x.contains_err(&"Some error message"), false);
353+
/// ```
354+
#[must_use]
355+
#[inline]
356+
#[unstable(feature = "result_contains_err", issue = "62358")]
357+
pub fn contains_err<F>(&self, f: &F) -> bool where F: PartialEq<E> {
358+
match self {
359+
Ok(_) => false,
360+
Err(e) => f == e
361+
}
362+
}
363+
312364
/////////////////////////////////////////////////////////////////////////
313365
// Adapter for each variant
314366
/////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)