3 use crate::marker::Destruct;
6 /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
7 /// or `None` otherwise.
9 /// Arguments passed to `then_some` are eagerly evaluated; if you are
10 /// passing the result of a function call, it is recommended to use
11 /// [`then`], which is lazily evaluated.
13 /// [`then`]: bool::then
18 /// assert_eq!(false.then_some(0), None);
19 /// assert_eq!(true.then_some(0), Some(0));
21 #[stable(feature = "bool_to_option", since = "1.62.0")]
22 #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
24 pub const fn then_some<T>(self, t: T) -> Option<T>
28 if self { Some(t) } else { None }
31 /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
32 /// or `None` otherwise.
37 /// assert_eq!(false.then(|| 0), None);
38 /// assert_eq!(true.then(|| 0), Some(0));
40 #[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
41 #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
43 pub const fn then<T, F>(self, f: F) -> Option<T>
45 F: ~const FnOnce() -> T,
48 if self { Some(f()) } else { None }