]>
Commit | Line | Data |
---|---|---|
e1599b0c XL |
1 | //! impl bool {} |
2 | ||
5e7ed085 FG |
3 | use crate::marker::Destruct; |
4 | ||
e1599b0c | 5 | impl bool { |
c295e0f8 XL |
6 | /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html), |
7 | /// or `None` otherwise. | |
e1599b0c | 8 | /// |
f2b60f7d FG |
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. | |
12 | /// | |
13 | /// [`then`]: bool::then | |
14 | /// | |
e1599b0c XL |
15 | /// # Examples |
16 | /// | |
17 | /// ``` | |
60c5eb7d XL |
18 | /// assert_eq!(false.then_some(0), None); |
19 | /// assert_eq!(true.then_some(0), Some(0)); | |
e1599b0c | 20 | /// ``` |
04454e1e | 21 | #[stable(feature = "bool_to_option", since = "1.62.0")] |
a2a8927a | 22 | #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")] |
e1599b0c | 23 | #[inline] |
a2a8927a XL |
24 | pub const fn then_some<T>(self, t: T) -> Option<T> |
25 | where | |
04454e1e | 26 | T: ~const Destruct, |
a2a8927a | 27 | { |
60c5eb7d | 28 | if self { Some(t) } else { None } |
e1599b0c XL |
29 | } |
30 | ||
c295e0f8 XL |
31 | /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html), |
32 | /// or `None` otherwise. | |
e1599b0c XL |
33 | /// |
34 | /// # Examples | |
35 | /// | |
36 | /// ``` | |
60c5eb7d XL |
37 | /// assert_eq!(false.then(|| 0), None); |
38 | /// assert_eq!(true.then(|| 0), Some(0)); | |
e1599b0c | 39 | /// ``` |
fc512014 | 40 | #[stable(feature = "lazy_bool_to_option", since = "1.50.0")] |
a2a8927a | 41 | #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")] |
e1599b0c | 42 | #[inline] |
a2a8927a XL |
43 | pub const fn then<T, F>(self, f: F) -> Option<T> |
44 | where | |
45 | F: ~const FnOnce() -> T, | |
04454e1e | 46 | F: ~const Destruct, |
a2a8927a | 47 | { |
60c5eb7d | 48 | if self { Some(f()) } else { None } |
e1599b0c XL |
49 | } |
50 | } |