about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2022-01-18 22:17:34 +0100
committerMara Bos <m-ou.se@m-ou.se>2022-01-18 22:17:34 +0100
commit282224edf19fb8a511c5a9e279f18b804ff61b25 (patch)
tree47dd234b33ecb2c83c3ddd143d3399404782a6e2
parent9ad5d82f822b3cb67637f11be2e65c5662b66ec0 (diff)
downloadrust-282224edf19fb8a511c5a9e279f18b804ff61b25.tar.gz
rust-282224edf19fb8a511c5a9e279f18b804ff61b25.zip
Add Option::is_some_with.
-rw-r--r--library/core/src/option.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index 8adfb6f4bcf..d9ee289f216 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -551,6 +551,27 @@ impl<T> Option<T> {
         matches!(*self, Some(_))
     }
 
+    /// Returns `true` if the option is a [`Some`] wrapping a value matching the predicate.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let x: Option<u32> = Some(2);
+    /// assert_eq!(x.is_some_with(|x| x > 1), true);
+    ///
+    /// let x: Option<u32> = Some(0);
+    /// assert_eq!(x.is_some_with(|x| x > 1), false);
+    ///
+    /// let x: Option<u32> = None;
+    /// assert_eq!(x.is_some_with(|x| x > 1), false);
+    /// ```
+    #[must_use]
+    #[inline]
+    #[unstable(feature = "is_some_with", issue = "none")]
+    pub fn is_some_with(&self, f: impl FnOnce(&T) -> bool) -> bool {
+        matches!(self, Some(x) if f(x))
+    }
+
     /// Returns `true` if the option is a [`None`] value.
     ///
     /// # Examples