Skip to content

Commit 282224e

Browse files
committed
Add Option::is_some_with.
1 parent 9ad5d82 commit 282224e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Diff for: library/core/src/option.rs

+21
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,27 @@ impl<T> Option<T> {
551551
matches!(*self, Some(_))
552552
}
553553

554+
/// Returns `true` if the option is a [`Some`] wrapping a value matching the predicate.
555+
///
556+
/// # Examples
557+
///
558+
/// ```
559+
/// let x: Option<u32> = Some(2);
560+
/// assert_eq!(x.is_some_with(|x| x > 1), true);
561+
///
562+
/// let x: Option<u32> = Some(0);
563+
/// assert_eq!(x.is_some_with(|x| x > 1), false);
564+
///
565+
/// let x: Option<u32> = None;
566+
/// assert_eq!(x.is_some_with(|x| x > 1), false);
567+
/// ```
568+
#[must_use]
569+
#[inline]
570+
#[unstable(feature = "is_some_with", issue = "none")]
571+
pub fn is_some_with(&self, f: impl FnOnce(&T) -> bool) -> bool {
572+
matches!(self, Some(x) if f(x))
573+
}
574+
554575
/// Returns `true` if the option is a [`None`] value.
555576
///
556577
/// # Examples

0 commit comments

Comments
 (0)