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: FnOnce() -> T>(self, f: F) -> Option<T>
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T>
Sourcepub fn ok_or<E>(self, err: E) -> Result<(), E>
🔬This is a nightly-only experimental API. (bool_to_result #142748)
pub fn ok_or<E>(self, err: E) -> Result<(), E>
bool_to_result #142748)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
#![feature(bool_to_result)]
let mut a = 0;
let mut function_with_side_effects = || { a += 1; };
assert!(true.ok_or(function_with_side_effects()).is_ok());
assert!(false.ok_or(function_with_side_effects()).is_err());
// `a` is incremented twice because the value passed to `ok_or` is
// evaluated eagerly.
assert_eq!(a, 2);Sourcepub fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E>
🔬This is a nightly-only experimental API. (bool_to_result #142748)
pub fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E>
bool_to_result #142748)Trait Implementations§
Source§impl AtomicPrimitive for bool
impl AtomicPrimitive for bool
Source§type AtomicInner = AtomicBool
type AtomicInner = AtomicBool
atomic_internals)Source§impl<T, const N: usize> BitAnd<Mask<T, N>> for bool
Available on non-crate feature ferrocene_certified only.
impl<T, const N: usize> BitAnd<Mask<T, N>> for bool
ferrocene_certified only.Source§impl<T, const N: usize> BitAnd<bool> for Mask<T, N>
Available on non-crate feature ferrocene_certified only.
impl<T, const N: usize> BitAnd<bool> for Mask<T, N>
ferrocene_certified only.1.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>
Available on non-crate feature ferrocene_certified only.
impl<T, const N: usize> BitAndAssign<bool> for Mask<T, N>
ferrocene_certified only.Source§fn bitand_assign(&mut self, rhs: bool)
fn bitand_assign(&mut self, rhs: bool)
&= operation. Read more1.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 moreSource§impl<T, const N: usize> BitOr<Mask<T, N>> for bool
Available on non-crate feature ferrocene_certified only.
impl<T, const N: usize> BitOr<Mask<T, N>> for bool
ferrocene_certified only.Source§impl<T, const N: usize> BitOr<bool> for Mask<T, N>
Available on non-crate feature ferrocene_certified only.
impl<T, const N: usize> BitOr<bool> for Mask<T, N>
ferrocene_certified only.1.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>
Available on non-crate feature ferrocene_certified only.
impl<T, const N: usize> BitOrAssign<bool> for Mask<T, N>
ferrocene_certified only.Source§fn bitor_assign(&mut self, rhs: bool)
fn bitor_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 moreSource§impl<T, const N: usize> BitXor<Mask<T, N>> for bool
Available on non-crate feature ferrocene_certified only.
impl<T, const N: usize> BitXor<Mask<T, N>> for bool
ferrocene_certified only.Source§impl<T, const N: usize> BitXor<bool> for Mask<T, N>
Available on non-crate feature ferrocene_certified only.
impl<T, const N: usize> BitXor<bool> for Mask<T, N>
ferrocene_certified only.1.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>
Available on non-crate feature ferrocene_certified only.
impl<T, const N: usize> BitXorAssign<bool> for Mask<T, N>
ferrocene_certified only.Source§fn bitxor_assign(&mut self, rhs: bool)
fn bitxor_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 moreSource§impl DisjointBitOr for bool
Available on non-crate feature ferrocene_certified only.
impl DisjointBitOr for bool
ferrocene_certified only.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
Available on non-crate feature ferrocene_certified only.
impl Distribution<bool> for RangeFull
ferrocene_certified only.1.24.0 (const: unstable) · Source§impl From<bool> for AtomicBool
Available on non-crate feature ferrocene_certified only.
impl From<bool> for AtomicBool
ferrocene_certified only.1.0.0 · Source§impl FromStr for bool
Available on non-crate feature ferrocene_certified only.
impl FromStr for bool
ferrocene_certified only.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.
Source§type Err = ParseBoolError
type Err = ParseBoolError
1.0.0 (const: unstable) · Source§impl PartialOrd for bool
impl PartialOrd for bool
impl ConstParamTy_ for bool
impl Copy for bool
impl Eq for bool
impl StructuralPartialEq for bool
impl UseCloned for bool
Auto Trait Implementations§
impl Freeze for bool
impl RefUnwindSafe for bool
impl Send for bool
impl Sync for bool
impl Unpin for bool
impl UnwindSafe for bool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
ferrocene_certified only.