1
Fork 0

Change is_some_and to take by value

This commit is contained in:
Cameron Steffen 2022-06-21 14:27:53 -05:00
parent 744e397d88
commit 2f83134e37
3 changed files with 22 additions and 13 deletions

View file

@ -562,19 +562,22 @@ impl<T> Option<T> {
/// #![feature(is_some_with)]
///
/// let x: Option<u32> = Some(2);
/// assert_eq!(x.is_some_and(|&x| x > 1), true);
/// assert_eq!(x.is_some_and(|x| x > 1), true);
///
/// let x: Option<u32> = Some(0);
/// assert_eq!(x.is_some_and(|&x| x > 1), false);
/// assert_eq!(x.is_some_and(|x| x > 1), false);
///
/// let x: Option<u32> = None;
/// assert_eq!(x.is_some_and(|&x| x > 1), false);
/// assert_eq!(x.is_some_and(|x| x > 1), false);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "is_some_with", issue = "93050")]
pub fn is_some_and(&self, f: impl FnOnce(&T) -> bool) -> bool {
matches!(self, Some(x) if f(x))
pub fn is_some_and(self, f: impl FnOnce(T) -> bool) -> bool {
match self {
None => false,
Some(x) => f(x),
}
}
/// Returns `true` if the option is a [`None`] value.