diff options
| author | Ralf Jung <post@ralfj.de> | 2024-06-12 15:19:40 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2024-06-12 16:12:29 +0200 |
| commit | 63bdcaa2d92bf5dba4dab23abb4e2b811e565438 (patch) | |
| tree | b6531de4f606152844343133ecb012375658293e | |
| parent | bbe9a9c20bac888efae2c7f033fb6cb3925a65b7 (diff) | |
| download | rust-63bdcaa2d92bf5dba4dab23abb4e2b811e565438.tar.gz rust-63bdcaa2d92bf5dba4dab23abb4e2b811e565438.zip | |
add is_none_or
| -rw-r--r-- | library/core/src/option.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs index e253cfd2822..d90b9cdb4b5 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -654,6 +654,32 @@ impl<T> Option<T> { !self.is_some() } + /// Returns `true` if the option is a [`None`] or the value inside of it matches a predicate. + /// + /// # Examples + /// + /// ``` + /// #![feature(is_none_or)] + /// + /// let x: Option<u32> = Some(2); + /// assert_eq!(x.is_none_or(|x| x > 1), true); + /// + /// let x: Option<u32> = Some(0); + /// assert_eq!(x.is_none_or(|x| x > 1), false); + /// + /// let x: Option<u32> = None; + /// assert_eq!(x.is_none_or(|x| x > 1), true); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "is_none_or", issue = "none")] + pub fn is_none_or(self, f: impl FnOnce(T) -> bool) -> bool { + match self { + None => true, + Some(x) => f(x), + } + } + ///////////////////////////////////////////////////////////////////////// // Adapter for working with references ///////////////////////////////////////////////////////////////////////// |
