rust/library/core/src/bool.rs

46 lines
1.2 KiB
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 {
/// Returns `Some(t)` if the `bool` is [`true`](../std/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")]
2021-12-14 16:11:23 +00:00
#[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
2019-09-07 14:49:27 +00:00
#[inline]
2021-12-14 16:11:23 +00:00
pub const fn then_some<T>(self, t: T) -> Option<T>
where
T: ~const Drop,
{
if self { Some(t) } else { None }
2019-09-07 14:49:27 +00:00
}
/// Returns `Some(f())` if the `bool` is [`true`](../std/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")]
2021-12-14 16:11:23 +00:00
#[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
2019-09-07 14:49:27 +00:00
#[inline]
2021-12-14 16:11:23 +00:00
pub const fn then<T, F>(self, f: F) -> Option<T>
where
F: ~const FnOnce() -> T,
F: ~const Drop,
{
if self { Some(f()) } else { None }
2019-09-07 14:49:27 +00:00
}
}