]> git.proxmox.com Git - rustc.git/blob - src/libcore/bool.rs
New upstream version 1.39.0+dfsg1
[rustc.git] / src / libcore / bool.rs
1 //! impl bool {}
2
3 #[cfg(not(bootstrap))]
4 #[lang = "bool"]
5 impl bool {
6 /// Returns `Some(t)` if the `bool` is `true`, or `None` otherwise.
7 ///
8 /// # Examples
9 ///
10 /// ```
11 /// #![feature(bool_to_option)]
12 ///
13 /// assert_eq!(false.then(0), None);
14 /// assert_eq!(true.then(0), Some(0));
15 /// ```
16 #[unstable(feature = "bool_to_option", issue = "64260")]
17 #[inline]
18 pub fn then<T>(self, t: T) -> Option<T> {
19 if self {
20 Some(t)
21 } else {
22 None
23 }
24 }
25
26 /// Returns `Some(f())` if the `bool` is `true`, or `None` otherwise.
27 ///
28 /// # Examples
29 ///
30 /// ```
31 /// #![feature(bool_to_option)]
32 ///
33 /// assert_eq!(false.then_with(|| 0), None);
34 /// assert_eq!(true.then_with(|| 0), Some(0));
35 /// ```
36 #[unstable(feature = "bool_to_option", issue = "64260")]
37 #[inline]
38 pub fn then_with<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
39 if self {
40 Some(f())
41 } else {
42 None
43 }
44 }
45 }