rust/library/core/src/bool.rs

35 lines
946 B
Rust
Raw Normal View History

2019-09-07 12:16:18 +00:00
//! impl bool {}
#[lang = "bool"]
2019-09-07 14:49:27 +00:00
impl bool {
2021-09-04 00:09:37 +00:00
/// Returns `Some(t)` if the `bool` is [`true`](keyword.true.html), or `None` otherwise.
2019-09-07 14:49:27 +00:00
///
/// # Examples
///
/// ```
/// #![feature(bool_to_option)]
///
2019-12-06 12:18:32 +00:00
/// assert_eq!(false.then_some(0), None);
/// assert_eq!(true.then_some(0), Some(0));
2019-09-07 14:49:27 +00:00
/// ```
#[unstable(feature = "bool_to_option", issue = "80967")]
2019-09-07 14:49:27 +00:00
#[inline]
2019-12-06 12:18:32 +00:00
pub fn then_some<T>(self, t: T) -> Option<T> {
if self { Some(t) } else { None }
2019-09-07 14:49:27 +00:00
}
2021-09-04 00:09:37 +00:00
/// Returns `Some` if the `bool` is [`true`](keyword.true.html), or `None` otherwise.
2019-09-07 14:49:27 +00:00
///
/// # Examples
///
/// ```
2019-12-06 12:18:32 +00:00
/// assert_eq!(false.then(|| 0), None);
/// assert_eq!(true.then(|| 0), Some(0));
2019-09-07 14:49:27 +00:00
/// ```
2020-11-22 13:25:19 +00:00
#[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
2019-09-07 14:49:27 +00:00
#[inline]
2019-12-06 12:18:32 +00:00
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
if self { Some(f()) } else { None }
2019-09-07 14:49:27 +00:00
}
}