Primitive Type bool
Expand description
The boolean type.
The bool
represents a value, which could only be either true
or false
. If you cast
a bool
into an integer, true
will be 1 and false
will be 0.
§Basic usage
bool
implements various traits, such as BitAnd
, BitOr
, Not
, etc.,
which allow us to perform boolean operations using &
, |
and !
.
if
requires a bool
value as its conditional. assert!
, which is an
important macro in testing, checks whether an expression is true
and panics
if it isn’t.
§Examples
A trivial example of the usage of bool
:
let praise_the_borrow_checker = true;
// using the `if` conditional
if praise_the_borrow_checker {
println!("oh, yeah!");
} else {
println!("what?!!");
}
// ... or, a match pattern
match praise_the_borrow_checker {
true => println!("keep praising!"),
false => println!("you should praise!"),
}
Also, since bool
implements the Copy
trait, we don’t
have to worry about the move semantics (just like the integer and float primitives).
Now an example of bool
cast to integer type:
Implementations§
Source§impl bool
impl bool
1.62.0 · Sourcepub fn then_some<T>(self, t: T) -> Option<T>
pub fn then_some<T>(self, t: T) -> Option<T>
1.50.0 · Sourcepub fn then<T, F>(self, f: F) -> Option<T>where
F: FnOnce() -> T,
pub fn then<T, F>(self, f: F) -> Option<T>where
F: FnOnce() -> T,
Sourcepub fn select_unpredictable<T>(self, true_val: T, false_val: T) -> T
🔬This is a nightly-only experimental API. (select_unpredictable
#133962)
pub fn select_unpredictable<T>(self, true_val: T, false_val: T) -> T
select_unpredictable
#133962)Returns either true_val
or false_val
depending on the value of
self
, with a hint to the compiler that self
is unlikely
to be correctly predicted by a CPU’s branch predictor.
This method is functionally equivalent to
fn select_unpredictable<T>(b: bool, true_val: T, false_val: T) -> T {
if b { true_val } else { false_val }
}
but might generate different assembly. In particular, on platforms with
a conditional move or select instruction (like cmov
on x86 or csel
on ARM) the optimizer might use these instructions to avoid branches,
which can benefit performance if the branch predictor is struggling
with predicting condition
, such as in an implementation of binary
search.
Note however that this lowering is not guaranteed (on any platform) and
should not be relied upon when trying to write constant-time code. Also
be aware that this lowering might decrease performance if condition
is well-predictable. It is advisable to perform benchmarks to tell if
this function is useful.
§Examples
Distribute values evenly between two buckets:
Trait Implementations§
1.22.0 · Source§impl BitAndAssign<&bool> for bool
impl BitAndAssign<&bool> for bool
Source§fn bitand_assign(&mut self, other: &bool)
fn bitand_assign(&mut self, other: &bool)
&=
operation. Read moreSource§impl<T, const N: usize> BitAndAssign<bool> for Mask<T, N>
impl<T, const N: usize> BitAndAssign<bool> for Mask<T, N>
Source§fn bitand_assign(&mut self, rhs: bool)
fn bitand_assign(&mut self, rhs: bool)
&=
operation. Read more1.8.0 · Source§impl BitAndAssign for bool
impl BitAndAssign for bool
Source§fn bitand_assign(&mut self, other: bool)
fn bitand_assign(&mut self, other: bool)
&=
operation. Read more1.22.0 · Source§impl BitOrAssign<&bool> for bool
impl BitOrAssign<&bool> for bool
Source§fn bitor_assign(&mut self, other: &bool)
fn bitor_assign(&mut self, other: &bool)
|=
operation. Read moreSource§impl<T, const N: usize> BitOrAssign<bool> for Mask<T, N>
impl<T, const N: usize> BitOrAssign<bool> for Mask<T, N>
Source§fn bitor_assign(&mut self, rhs: bool)
fn bitor_assign(&mut self, rhs: bool)
|=
operation. Read more1.8.0 · Source§impl BitOrAssign for bool
impl BitOrAssign for bool
Source§fn bitor_assign(&mut self, other: bool)
fn bitor_assign(&mut self, other: bool)
|=
operation. Read more1.22.0 · Source§impl BitXorAssign<&bool> for bool
impl BitXorAssign<&bool> for bool
Source§fn bitxor_assign(&mut self, other: &bool)
fn bitxor_assign(&mut self, other: &bool)
^=
operation. Read moreSource§impl<T, const N: usize> BitXorAssign<bool> for Mask<T, N>
impl<T, const N: usize> BitXorAssign<bool> for Mask<T, N>
Source§fn bitxor_assign(&mut self, rhs: bool)
fn bitxor_assign(&mut self, rhs: bool)
^=
operation. Read more1.8.0 · Source§impl BitXorAssign for bool
impl BitXorAssign for bool
Source§fn bitxor_assign(&mut self, other: bool)
fn bitxor_assign(&mut self, other: bool)
^=
operation. Read more1.24.0 · Source§impl From<bool> for AtomicBool
impl From<bool> for AtomicBool
1.0.0 · Source§impl FromStr for bool
impl FromStr for bool
Source§fn from_str(s: &str) -> Result<bool, ParseBoolError>
fn from_str(s: &str) -> Result<bool, ParseBoolError>
Parse a bool
from a string.
The only accepted values are "true"
and "false"
. Any other input
will return an error.
§Examples
use std::str::FromStr;
assert_eq!(FromStr::from_str("true"), Ok(true));
assert_eq!(FromStr::from_str("false"), Ok(false));
assert!(<bool as FromStr>::from_str("not even a boolean").is_err());
Note, in many cases, the .parse()
method on str
is more proper.