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