]> git.lizzy.rs Git - rust.git/blob - library/core/src/bool.rs
Auto merge of #94702 - b-naber:static-refs-mir, r=lcnr
[rust.git] / library / core / src / bool.rs
1 //! impl bool {}
2
3 #[lang = "bool"]
4 impl bool {
5     /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
6     /// or `None` otherwise.
7     ///
8     /// # Examples
9     ///
10     /// ```
11     /// #![feature(bool_to_option)]
12     ///
13     /// assert_eq!(false.then_some(0), None);
14     /// assert_eq!(true.then_some(0), Some(0));
15     /// ```
16     #[unstable(feature = "bool_to_option", issue = "80967")]
17     #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
18     #[inline]
19     pub const fn then_some<T>(self, t: T) -> Option<T>
20     where
21         T: ~const Drop,
22     {
23         if self { Some(t) } else { None }
24     }
25
26     /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
27     /// or `None` otherwise.
28     ///
29     /// # Examples
30     ///
31     /// ```
32     /// assert_eq!(false.then(|| 0), None);
33     /// assert_eq!(true.then(|| 0), Some(0));
34     /// ```
35     #[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
36     #[rustc_const_unstable(feature = "const_bool_to_option", issue = "91917")]
37     #[inline]
38     pub const fn then<T, F>(self, f: F) -> Option<T>
39     where
40         F: ~const FnOnce() -> T,
41         F: ~const Drop,
42     {
43         if self { Some(f()) } else { None }
44     }
45 }