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 (const: unstable) ยท Sourcepub fn then_some<T>(self, t: T) -> Option<T>
pub fn then_some<T>(self, t: T) -> Option<T>
1.50.0 (const: unstable) ยท Sourcepub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T>
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T>
1.98.0 (const: unstable) ยท Sourcepub fn ok_or<E>(self, err: E) -> Result<(), E>
pub fn ok_or<E>(self, err: E) -> Result<(), E>
Returns Ok(()) if the bool is true,
or Err(err) otherwise.
Arguments passed to ok_or are eagerly evaluated; if you are
passing the result of a function call, it is recommended to use
ok_or_else, which is lazily evaluated.
ยงExamples
1.98.0 (const: unstable) ยท Sourcepub fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E>
pub fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E>
Trait Implementationsยง
Sourceยงimpl AtomicPrimitive for bool
impl AtomicPrimitive for bool
1.8.0 (const: unstable) ยท 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 (const: unstable) ยท 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>where
T: MaskElement,
impl<T, const N: usize> BitAndAssign<bool> for Mask<T, N>where
T: MaskElement,
Sourceยงfn bitand_assign(&mut self, rhs: bool)
fn bitand_assign(&mut self, rhs: bool)
&= operation. Read more1.8.0 (const: unstable) ยท 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 (const: unstable) ยท 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>where
T: MaskElement,
impl<T, const N: usize> BitOrAssign<bool> for Mask<T, N>where
T: MaskElement,
Sourceยงfn bitor_assign(&mut self, rhs: bool)
fn bitor_assign(&mut self, rhs: bool)
|= operation. Read more1.8.0 (const: unstable) ยท 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.22.0 (const: unstable) ยท 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>where
T: MaskElement,
impl<T, const N: usize> BitXorAssign<bool> for Mask<T, N>where
T: MaskElement,
Sourceยงfn bitxor_assign(&mut self, rhs: bool)
fn bitxor_assign(&mut self, rhs: bool)
^= operation. Read moreimpl ConstParamTy_ for bool
impl Copy for bool
Sourceยงimpl DisjointBitOr for bool
impl DisjointBitOr for bool
Sourceยงunsafe fn disjoint_bitor(self, other: Self) -> Self
unsafe fn disjoint_bitor(self, other: Self) -> Self
core_intrinsics_fallbacks)super::disjoint_bitor; we just need the trait indirection to handle
different types since calling intrinsics with generics doesnโt work.Sourceยงimpl Distribution<bool> for RangeFull
impl Distribution<bool> for RangeFull
impl Eq for bool
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.