core/
bool.rs

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