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    /// assert_eq!(false.ok_or(0), Err(0));
82    /// assert_eq!(true.ok_or(0), Ok(()));
83    /// ```
84    ///
85    /// ```
86    /// let mut a = 0;
87    /// let mut function_with_side_effects = || { a += 1; };
88    ///
89    /// assert!(true.ok_or(function_with_side_effects()).is_ok());
90    /// assert!(false.ok_or(function_with_side_effects()).is_err());
91    ///
92    /// // `a` is incremented twice because the value passed to `ok_or` is
93    /// // evaluated eagerly.
94    /// assert_eq!(a, 2);
95    /// ```
96    #[stable(feature = "bool_to_result", since = "CURRENT_RUSTC_VERSION")]
97    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
98    #[inline]
99    pub const fn ok_or<E: [const] Destruct>(self, err: E) -> Result<(), E> {
100        if self { Ok(()) } else { Err(err) }
101    }
102
103    /// Returns `Ok(())` if the `bool` is [`true`](../std/keyword.true.html),
104    /// or `Err(f())` otherwise.
105    ///
106    /// # Examples
107    ///
108    /// ```
109    /// assert_eq!(false.ok_or_else(|| 0), Err(0));
110    /// assert_eq!(true.ok_or_else(|| 0), Ok(()));
111    /// ```
112    ///
113    /// ```
114    /// let mut a = 0;
115    ///
116    /// assert!(true.ok_or_else(|| { a += 1; }).is_ok());
117    /// assert!(false.ok_or_else(|| { a += 1; }).is_err());
118    ///
119    /// // `a` is incremented once because the closure is evaluated lazily by
120    /// // `ok_or_else`.
121    /// assert_eq!(a, 1);
122    /// ```
123    #[stable(feature = "bool_to_result", since = "CURRENT_RUSTC_VERSION")]
124    #[rustc_const_unstable(feature = "const_bool", issue = "151531")]
125    #[inline]
126    pub const fn ok_or_else<E, F: [const] FnOnce() -> E + [const] Destruct>(
127        self,
128        f: F,
129    ) -> Result<(), E> {
130        if self { Ok(()) } else { Err(f()) }
131    }
132}