提交 aaebae97 编写于 作者: M Mara Bos

Add Result::{is_ok_with, is_err_with}.

上级 282224ed
......@@ -542,6 +542,27 @@ pub const fn is_ok(&self) -> bool {
matches!(*self, Ok(_))
}
/// Returns `true` if the result is [`Ok`] wrapping a value matching the predicate.
///
/// # Examples
///
/// ```
/// let x: Result<u32, &str> = Ok(2);
/// assert_eq!(x.is_ok_with(|x| x > 1), true);
///
/// let x: Result<u32, &str> = Ok(0);
/// assert_eq!(x.is_ok_with(|x| x > 1), false);
///
/// let x: Result<u32, &str> = Err("hey");
/// assert_eq!(x.is_ok_with(|x| x > 1), false);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "is_some_with", issue = "none")]
pub fn is_ok_with(&self, f: impl FnOnce(&T) -> bool) -> bool {
matches!(self, Ok(x) if f(x))
}
/// Returns `true` if the result is [`Err`].
///
/// # Examples
......@@ -563,6 +584,27 @@ pub const fn is_err(&self) -> bool {
!self.is_ok()
}
/// Returns `true` if the result is [`Err`] wrapping a value matching the predicate.
///
/// # Examples
///
/// ```
/// let x: Result<u32, &str> = Err("abc");
/// assert_eq!(x.is_err_with(|x| x.len() > 1), true);
///
/// let x: Result<u32, &str> = Err("");
/// assert_eq!(x.is_ok_with(|x| x.len() > 1), false);
///
/// let x: Result<u32, &str> = Ok(123);
/// assert_eq!(x.is_ok_with(|x| x.len() > 1), false);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "is_some_with", issue = "none")]
pub fn is_err_with(&self, f: impl FnOnce(&E) -> bool) -> bool {
matches!(self, Err(x) if f(x))
}
/////////////////////////////////////////////////////////////////////////
// Adapter for each variant
/////////////////////////////////////////////////////////////////////////
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册