]> git.lizzy.rs Git - rust.git/blob - src/libcore/bool.rs
Auto merge of #66927 - RalfJung:engines-dont-panic, r=oli-obk
[rust.git] / src / libcore / bool.rs
1 //! impl bool {}
2
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     ///
12     /// assert_eq!(false.then_some(0), None);
13     /// assert_eq!(true.then_some(0), Some(0));
14     /// ```
15     #[unstable(feature = "bool_to_option", issue = "64260")]
16     #[inline]
17     pub fn then_some<T>(self, t: T) -> Option<T> {
18         if self {
19             Some(t)
20         } else {
21             None
22         }
23     }
24
25     /// Returns `Some(f())` if the `bool` is `true`, or `None` otherwise.
26     ///
27     /// # Examples
28     ///
29     /// ```
30     /// #![feature(bool_to_option)]
31     ///
32     /// assert_eq!(false.then(|| 0), None);
33     /// assert_eq!(true.then(|| 0), Some(0));
34     /// ```
35     #[unstable(feature = "bool_to_option", issue = "64260")]
36     #[inline]
37     pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
38         if self {
39             Some(f())
40         } else {
41             None
42         }
43     }
44 }