]>
git.proxmox.com Git - rustc.git/blob - library/core/src/bool.rs
5 /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
6 /// or `None` otherwise.
11 /// #![feature(bool_to_option)]
13 /// assert_eq!(false.then_some(0), None);
14 /// assert_eq!(true.then_some(0), Some(0));
16 #[unstable(feature = "bool_to_option", issue = "80967")]
18 pub fn then_some
<T
>(self, t
: T
) -> Option
<T
> {
19 if self { Some(t) }
else { None }
22 /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
23 /// or `None` otherwise.
28 /// assert_eq!(false.then(|| 0), None);
29 /// assert_eq!(true.then(|| 0), Some(0));
31 #[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
33 pub fn then
<T
, F
: FnOnce() -> T
>(self, f
: F
) -> Option
<T
> {
34 if self { Some(f()) }
else { None }