]>
Commit | Line | Data |
---|---|---|
e1599b0c XL |
1 | //! impl bool {} |
2 | ||
e1599b0c XL |
3 | #[lang = "bool"] |
4 | impl bool { | |
5 | /// Returns `Some(t)` if the `bool` is `true`, or `None` otherwise. | |
6 | /// | |
7 | /// # Examples | |
8 | /// | |
9 | /// ``` | |
10 | /// #![feature(bool_to_option)] | |
11 | /// | |
60c5eb7d XL |
12 | /// assert_eq!(false.then_some(0), None); |
13 | /// assert_eq!(true.then_some(0), Some(0)); | |
e1599b0c XL |
14 | /// ``` |
15 | #[unstable(feature = "bool_to_option", issue = "64260")] | |
16 | #[inline] | |
60c5eb7d XL |
17 | pub fn then_some<T>(self, t: T) -> Option<T> { |
18 | if self { Some(t) } else { None } | |
e1599b0c XL |
19 | } |
20 | ||
21 | /// Returns `Some(f())` if the `bool` is `true`, or `None` otherwise. | |
22 | /// | |
23 | /// # Examples | |
24 | /// | |
25 | /// ``` | |
26 | /// #![feature(bool_to_option)] | |
27 | /// | |
60c5eb7d XL |
28 | /// assert_eq!(false.then(|| 0), None); |
29 | /// assert_eq!(true.then(|| 0), Some(0)); | |
e1599b0c XL |
30 | /// ``` |
31 | #[unstable(feature = "bool_to_option", issue = "64260")] | |
32 | #[inline] | |
60c5eb7d XL |
33 | pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> { |
34 | if self { Some(f()) } else { None } | |
e1599b0c XL |
35 | } |
36 | } |