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.
let bool_val = true & false | false;
assert!(!bool_val);
RunExamples
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!"),
}
RunAlso, 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:
assert_eq!(true as i32, 1);
assert_eq!(false as i32, 0);
RunImplementations
sourceimpl bool
impl bool
1.62.0 (const: unstable) · sourcepub fn then_some<T>(self, t: T) -> Option<T>
pub fn then_some<T>(self, t: T) -> Option<T>
Returns Some(t)
if the bool
is true
,
or None
otherwise.
Arguments passed to then_some
are eagerly evaluated; if you are
passing the result of a function call, it is recommended to use
then
, which is lazily evaluated.
Examples
assert_eq!(false.then_some(0), None);
assert_eq!(true.then_some(0), Some(0));
RunTrait Implementations
sourceimpl<T, const LANES: usize> BitAnd<Mask<T, LANES>> for boolwhere
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitAnd<Mask<T, LANES>> for boolwhere
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourceimpl<T, const LANES: usize> BitAnd<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitAnd<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
1.22.0 (const: unstable) · sourceimpl BitAndAssign<&bool> for bool
impl BitAndAssign<&bool> for bool
const: unstable · sourcefn bitand_assign(&mut self, other: &bool)
fn bitand_assign(&mut self, other: &bool)
Performs the &=
operation. Read more
sourceimpl<T, const LANES: usize> BitAndAssign<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitAndAssign<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourcefn bitand_assign(&mut self, rhs: bool)
fn bitand_assign(&mut self, rhs: bool)
Performs the &=
operation. Read more
1.8.0 (const: unstable) · sourceimpl BitAndAssign<bool> for bool
impl BitAndAssign<bool> for bool
const: unstable · sourcefn bitand_assign(&mut self, other: bool)
fn bitand_assign(&mut self, other: bool)
Performs the &=
operation. Read more
sourceimpl<T, const LANES: usize> BitOr<Mask<T, LANES>> for boolwhere
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitOr<Mask<T, LANES>> for boolwhere
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourceimpl<T, const LANES: usize> BitOr<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitOr<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
1.22.0 (const: unstable) · sourceimpl BitOrAssign<&bool> for bool
impl BitOrAssign<&bool> for bool
const: unstable · sourcefn bitor_assign(&mut self, other: &bool)
fn bitor_assign(&mut self, other: &bool)
Performs the |=
operation. Read more
sourceimpl<T, const LANES: usize> BitOrAssign<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitOrAssign<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourcefn bitor_assign(&mut self, rhs: bool)
fn bitor_assign(&mut self, rhs: bool)
Performs the |=
operation. Read more
1.8.0 (const: unstable) · sourceimpl BitOrAssign<bool> for bool
impl BitOrAssign<bool> for bool
const: unstable · sourcefn bitor_assign(&mut self, other: bool)
fn bitor_assign(&mut self, other: bool)
Performs the |=
operation. Read more
sourceimpl<T, const LANES: usize> BitXor<Mask<T, LANES>> for boolwhere
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitXor<Mask<T, LANES>> for boolwhere
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourceimpl<T, const LANES: usize> BitXor<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitXor<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
1.22.0 (const: unstable) · sourceimpl BitXorAssign<&bool> for bool
impl BitXorAssign<&bool> for bool
const: unstable · sourcefn bitxor_assign(&mut self, other: &bool)
fn bitxor_assign(&mut self, other: &bool)
Performs the ^=
operation. Read more
sourceimpl<T, const LANES: usize> BitXorAssign<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitXorAssign<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
sourcefn bitxor_assign(&mut self, rhs: bool)
fn bitxor_assign(&mut self, rhs: bool)
Performs the ^=
operation. Read more
1.8.0 (const: unstable) · sourceimpl BitXorAssign<bool> for bool
impl BitXorAssign<bool> for bool
const: unstable · sourcefn bitxor_assign(&mut self, other: bool)
fn bitxor_assign(&mut self, other: bool)
Performs the ^=
operation. Read more
1.24.0 (const: unstable) · sourceimpl From<bool> for AtomicBool
impl From<bool> for AtomicBool
sourceimpl FromStr for bool
impl FromStr for bool
sourcefn from_str(s: &str) -> Result<bool, ParseBoolError>
fn from_str(s: &str) -> Result<bool, ParseBoolError>
Parse a bool
from a string.
Yields a Result<bool, ParseBoolError>
, because s
may or may not
actually be parseable.
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());
RunNote, in many cases, the .parse()
method on str
is more proper.
assert_eq!("true".parse(), Ok(true));
assert_eq!("false".parse(), Ok(false));
assert!("not even a boolean".parse::<bool>().is_err());
Runtype Err = ParseBoolError
type Err = ParseBoolError
The associated error which can be returned from parsing.
const: unstable · sourceimpl Ord for bool
impl Ord for bool
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
const: unstable · sourceimpl PartialEq<bool> for bool
impl PartialEq<bool> for bool
const: unstable · sourceimpl PartialOrd<bool> for bool
impl PartialOrd<bool> for bool
const: unstable · sourcefn partial_cmp(&self, other: &bool) -> Option<Ordering>
fn partial_cmp(&self, other: &bool) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Copy for bool
impl Eq for bool
Auto Trait Implementations
impl RefUnwindSafe for bool
impl Send for bool
impl Sync for bool
impl Unpin for bool
impl UnwindSafe for bool
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more