Skip to main content

core/
bool.rs

1//! impl bool {}
2
3use crate::marker::Destruct;
4
5impl bool {
6    /// Returns `Some(t)` if the `bool` is [`true`](../std/keyword.true.html),
7    /// or `None` otherwise.
8    ///
9    /// Arguments passed to `then_some` are eagerly evaluated; if you are
10    /// passing the result of a function call, it is recommended to use
11    /// [`then`], which is lazily evaluated.
12    ///
13    /// [`then`]: bool::then
14    ///
15    /// # Examples
16    ///
17    /// ```
18    /// assert_eq!(false.then_some(0), None);
19    /// assert_eq!(true.then_some(0), Some(0));
20    /// ```
21    ///
22    /// ```
23    /// let mut a = 0;
24    /// let mut function_with_side_effects = || { a += 1; };
25    ///
26    /// true.then_some(function_with_side_effects());
27    /// false.then_some(function_with_side_effects());
28    ///
29    /// // `a` is incremented twice because the value passed to `then_some` is
30    /// // evaluated eagerly.
31    /// assert_eq!(a, 2);
32    /// ```
33    #[stable(feature = "bool_to_option", since = "1.62.0")]
34    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
35    #[inline]
36    pub const fn then_some<T: [const] Destruct>(self, t: T) -> Option<T> {
37        if self { Some(t) } else { None }
38    }
39
40    /// Returns `Some(f())` if the `bool` is [`true`](../std/keyword.true.html),
41    /// or `None` otherwise.
42    ///
43    /// # Examples
44    ///
45    /// ```
46    /// assert_eq!(false.then(|| 0), None);
47    /// assert_eq!(true.then(|| 0), Some(0));
48    /// ```
49    ///
50    /// ```
51    /// let mut a = 0;
52    ///
53    /// true.then(|| { a += 1; });
54    /// false.then(|| { a += 1; });
55    ///
56    /// // `a` is incremented once because the closure is evaluated lazily by
57    /// // `then`.
58    /// assert_eq!(a, 1);
59    /// ```
60    #[doc(alias = "then_with")]
61    #[stable(feature = "lazy_bool_to_option", since = "1.50.0")]
62    #[rustc_diagnostic_item = "bool_then"]
63    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
64    #[inline]
65    pub const fn then<T, F: [const] FnOnce() -> T + [const] Destruct>(self, f: F) -> Option<T> {
66        if self { Some(f()) } else { None }
67    }
68
69    /// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
70    /// or `Err(err)` otherwise.
71    ///
72    /// Arguments passed to `ok_or` are eagerly evaluated; if you are
73    /// passing the result of a function call, it is recommended to use
74    /// [`ok_or_else`], which is lazily evaluated.
75    ///
76    /// [`ok_or_else`]: bool::ok_or_else
77    ///
78    /// # Examples
79    ///
80    /// ```
81    /// #![feature(bool_to_result)]
82    ///
83    /// assert_eq!(false.ok_or(0), Err(0));
84    /// assert_eq!(true.ok_or(0), Ok(()));
85    /// ```
86    ///
87    /// ```
88    /// #![feature(bool_to_result)]
89    ///
90    /// let mut a = 0;
91    /// let mut function_with_side_effects = || { a += 1; };
92    ///
93    /// assert!(true.ok_or(function_with_side_effects()).is_ok());
94    /// assert!(false.ok_or(function_with_side_effects()).is_err());
95    ///
96    /// // `a` is incremented twice because the value passed to `ok_or` is
97    /// // evaluated eagerly.
98    /// assert_eq!(a, 2);
99    /// ```
100    #[unstable(feature = "bool_to_result", issue = "142748")]
101    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
102    #[inline]
103    pub const fn ok_or<E: [const] Destruct>(self, err: E) -> Result<(), E> {
104        if self { Ok(()) } else { Err(err) }
105    }
106
107    /// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
108    /// or `Err(f())` otherwise.
109    ///
110    /// # Examples
111    ///
112    /// ```
113    /// #![feature(bool_to_result)]
114    ///
115    /// assert_eq!(false.ok_or_else(|| 0), Err(0));
116    /// assert_eq!(true.ok_or_else(|| 0), Ok(()));
117    /// ```
118    ///
119    /// ```
120    /// #![feature(bool_to_result)]
121    ///
122    /// let mut a = 0;
123    ///
124    /// assert!(true.ok_or_else(|| { a += 1; }).is_ok());
125    /// assert!(false.ok_or_else(|| { a += 1; }).is_err());
126    ///
127    /// // `a` is incremented once because the closure is evaluated lazily by
128    /// // `ok_or_else`.
129    /// assert_eq!(a, 1);
130    /// ```
131    #[unstable(feature = "bool_to_result", issue = "142748")]
132    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
133    #[inline]
134    pub const fn ok_or_else<E, F: [const] FnOnce() -> E + [const] Destruct>(
135        self,
136        f: F,
137    ) -> Result<(), E> {
138        if self { Ok(()) } else { Err(f()) }
139    }
140}