Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Option::is_none_or #100602

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,33 @@ impl<T> Option<T> {
!self.is_some()
}

/// Returns `true` if the option is a [`None`] or the [`Some`] value
/// inside of it matches a predicate.
///
/// # Examples
///
/// ```
/// #![feature(is_some_with)]
///
/// 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_some_with", issue = "93050")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[unstable(feature = "is_some_with", issue = "93050")]
#[unstable(feature = "option_is_none_or", issue = "none")]

For now. But you can wait for the ACP to be resolved.

pub fn is_none_or(&self, f: impl FnOnce(&T) -> bool) -> bool {
match self {
Some(x) => f(x),
None => true,
}
}

/////////////////////////////////////////////////////////////////////////
// Adapter for working with references
/////////////////////////////////////////////////////////////////////////
Expand Down