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));
24 /// let mut function_with_side_effects = || { a += 1; };
26 /// true.then_some(function_with_side_effects());
27 /// false.then_some(function_with_side_effects());
29 /// // `a` is incremented twice because the value passed to `then_some` is
30 /// // evaluated eagerly.
33 #[stable(feature = "bool_to_option", since = "1.62.0")]
34 #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
36 pub const fn then_some<T>(self, t: T) -> Option<T>
40 if self { Some(t) } else { None }
43 /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
44 /// or `None` otherwise.
49 /// assert_eq!(false.then(|| 0), None);
50 /// assert_eq!(true.then(|| 0), Some(0));
56 /// true.then(|| { a += 1; });
57 /// false.then(|| { a += 1; });
59 /// // `a` is incremented once because the closure is evaluated lazily by
63 #[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
64 #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
66 pub const fn then<T, F>(self, f: F) -> Option<T>
68 F: ~const FnOnce() -> T,
71 if self { Some(f()) } else { None }