On this page
Primitive Type bool
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);
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:
assert_eq!(true as i32, 1);
assert_eq!(false as i32, 0);
Implementations
impl bool
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));
let mut a = 0;
let mut function_with_side_effects = || { a += 1; };
true.then_some(function_with_side_effects());
false.then_some(function_with_side_effects());
// `a` is incremented twice because the value passed to `then_some` is
// evaluated eagerly.
assert_eq!(a, 2);
pub fn then<T, F>(self, f: F) -> Option<T>
where
F: FnOnce() -> T,
Returns Some(f())
if the bool
is true
, or None
otherwise.
Examples
assert_eq!(false.then(|| 0), None);
assert_eq!(true.then(|| 0), Some(0));
let mut a = 0;
true.then(|| { a += 1; });
false.then(|| { a += 1; });
// `a` is incremented once because the closure is evaluated lazily by
// `then`.
assert_eq!(a, 1);
Trait Implementations
impl BitAnd<&bool> for &bool
type Output = <bool as BitAnd>::Output
&
operator.
fn bitand(self, other: &bool) -> <bool as BitAnd>::Output
&
operation. Read more
impl BitAnd<&bool> for bool
type Output = <bool as BitAnd>::Output
&
operator.
fn bitand(self, other: &bool) -> <bool as BitAnd>::Output
&
operation. Read more
impl<T, const LANES: usize> BitAnd<Mask<T, LANES>> for bool
where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
type Output = Mask<T, LANES>
&
operator.
fn bitand(self, rhs: Mask<T, LANES>) -> Mask<T, LANES>
&
operation. Read more
impl<'a> BitAnd<bool> for &'a bool
type Output = <bool as BitAnd>::Output
&
operator.
fn bitand(self, other: bool) -> <bool as BitAnd>::Output
&
operation. Read more
impl<T, const LANES: usize> BitAnd<bool> for Mask<T, LANES>
where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
type Output = Mask<T, LANES>
&
operator.
fn bitand(self, rhs: bool) -> Mask<T, LANES>
&
operation. Read more
impl BitAnd for bool
type Output = bool
&
operator.
fn bitand(self, rhs: bool) -> bool
&
operation. Read more
impl BitAndAssign<&bool> for bool
impl<T, const LANES: usize> BitAndAssign<bool> for Mask<T, LANES>
where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl BitAndAssign for bool
impl BitOr<&bool> for &bool
type Output = <bool as BitOr>::Output
|
operator.
fn bitor(self, other: &bool) -> <bool as BitOr>::Output
|
operation. Read more
impl BitOr<&bool> for bool
type Output = <bool as BitOr>::Output
|
operator.
fn bitor(self, other: &bool) -> <bool as BitOr>::Output
|
operation. Read more
impl<T, const LANES: usize> BitOr<Mask<T, LANES>> for bool
where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
type Output = Mask<T, LANES>
|
operator.
fn bitor(self, rhs: Mask<T, LANES>) -> Mask<T, LANES>
|
operation. Read more
impl<'a> BitOr<bool> for &'a bool
type Output = <bool as BitOr>::Output
|
operator.
fn bitor(self, other: bool) -> <bool as BitOr>::Output
|
operation. Read more
impl<T, const LANES: usize> BitOr<bool> for Mask<T, LANES>
where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
type Output = Mask<T, LANES>
|
operator.
fn bitor(self, rhs: bool) -> Mask<T, LANES>
|
operation. Read more
impl BitOr for bool
type Output = bool
|
operator.
fn bitor(self, rhs: bool) -> bool
|
operation. Read more
impl BitOrAssign<&bool> for bool
impl<T, const LANES: usize> BitOrAssign<bool> for Mask<T, LANES>
where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl BitOrAssign for bool
impl BitXor<&bool> for &bool
type Output = <bool as BitXor>::Output
^
operator.
fn bitxor(self, other: &bool) -> <bool as BitXor>::Output
^
operation. Read more
impl BitXor<&bool> for bool
type Output = <bool as BitXor>::Output
^
operator.
fn bitxor(self, other: &bool) -> <bool as BitXor>::Output
^
operation. Read more
impl<T, const LANES: usize> BitXor<Mask<T, LANES>> for bool
where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
type Output = Mask<T, LANES>
^
operator.
fn bitxor(self, rhs: Mask<T, LANES>) -> <bool as BitXor<Mask<T, LANES>>>::Output
^
operation. Read more
impl<'a> BitXor<bool> for &'a bool
type Output = <bool as BitXor>::Output
^
operator.
fn bitxor(self, other: bool) -> <bool as BitXor>::Output
^
operation. Read more
impl<T, const LANES: usize> BitXor<bool> for Mask<T, LANES>
where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
type Output = Mask<T, LANES>
^
operator.
fn bitxor(self, rhs: bool) -> <Mask<T, LANES> as BitXor<bool>>::Output
^
operation. Read more
impl BitXor for bool
type Output = bool
^
operator.
fn bitxor(self, other: bool) -> bool
^
operation. Read more
impl BitXorAssign<&bool> for bool
impl<T, const LANES: usize> BitXorAssign<bool> for Mask<T, LANES>
where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl BitXorAssign for bool
impl Clone for bool
fn clone(&self) -> bool
fn clone_from(&mut self, source: &Self)
source
. Read more
impl Debug for bool
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl Default for bool
fn default() -> bool
Returns the default value of false
impl Display for bool
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl From<bool> for AtomicBool
fn from(b: bool) -> AtomicBool
Converts a bool
into an AtomicBool
.
Examples
use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{atomic_bool:?}"), "true")
impl From<bool> for f32
fn from(small: bool) -> f32
Converts bool
to f32
losslessly. The resulting value is positive 0.0
for false
and 1.0
for true
values.
Examples
let x: f32 = false.into();
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());
let y: f32 = true.into();
assert_eq!(y, 1.0);
impl From<bool> for f64
fn from(small: bool) -> f64
Converts bool
to f64
losslessly. The resulting value is positive 0.0
for false
and 1.0
for true
values.
Examples
let x: f64 = false.into();
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());
let y: f64 = true.into();
assert_eq!(y, 1.0);
impl From<bool> for i128
fn from(small: bool) -> i128
Converts a bool
to a i128
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i128::from(true), 1);
assert_eq!(i128::from(false), 0);
impl From<bool> for i16
fn from(small: bool) -> i16
Converts a bool
to a i16
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i16::from(true), 1);
assert_eq!(i16::from(false), 0);
impl From<bool> for i32
fn from(small: bool) -> i32
Converts a bool
to a i32
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i32::from(true), 1);
assert_eq!(i32::from(false), 0);
impl From<bool> for i64
fn from(small: bool) -> i64
Converts a bool
to a i64
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i64::from(true), 1);
assert_eq!(i64::from(false), 0);
impl From<bool> for i8
fn from(small: bool) -> i8
Converts a bool
to a i8
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i8::from(true), 1);
assert_eq!(i8::from(false), 0);
impl From<bool> for isize
fn from(small: bool) -> isize
Converts a bool
to a isize
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(isize::from(true), 1);
assert_eq!(isize::from(false), 0);
impl From<bool> for u128
fn from(small: bool) -> u128
Converts a bool
to a u128
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u128::from(true), 1);
assert_eq!(u128::from(false), 0);
impl From<bool> for u16
fn from(small: bool) -> u16
Converts a bool
to a u16
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u16::from(true), 1);
assert_eq!(u16::from(false), 0);
impl From<bool> for u32
fn from(small: bool) -> u32
Converts a bool
to a u32
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u32::from(true), 1);
assert_eq!(u32::from(false), 0);
impl From<bool> for u64
fn from(small: bool) -> u64
Converts a bool
to a u64
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u64::from(true), 1);
assert_eq!(u64::from(false), 0);
impl From<bool> for u8
fn from(small: bool) -> u8
Converts a bool
to a u8
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u8::from(true), 1);
assert_eq!(u8::from(false), 0);
impl From<bool> for usize
fn from(small: bool) -> usize
Converts a bool
to a usize
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(usize::from(true), 1);
assert_eq!(usize::from(false), 0);
impl FromStr for bool
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.
assert_eq!("true".parse(), Ok(true));
assert_eq!("false".parse(), Ok(false));
assert!("not even a boolean".parse::<bool>().is_err());
type Err = ParseBoolError
impl Hash for bool
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
fn hash_slice<H>(data: &[Self], state: &mut H)
where
H: Hasher,
Self: Sized,
impl Not for &bool
type Output = <bool as Not>::Output
!
operator.
fn not(self) -> <bool as Not>::Output
!
operation. Read more
impl Not for bool
type Output = bool
!
operator.
fn not(self) -> bool
!
operation. Read more
impl Ord for bool
fn cmp(&self, other: &bool) -> Ordering
fn min(self, other: bool) -> bool
fn max(self, other: bool) -> bool
fn clamp(self, min: bool, max: bool) -> bool
impl PartialEq for bool
fn eq(&self, other: &bool) -> bool
self
and other
values to be equal, and is used by ==
.
fn ne(&self, other: &bool) -> bool
!=
. The default implementation is almost always sufficient, and should not be overridden without very good reason.
impl PartialOrd for bool
fn partial_cmp(&self, other: &bool) -> Option<Ordering>
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
self
and other
) and is used by the >=
operator. Read more
impl ConstParamTy for bool
impl Copy for bool
impl Eq for bool
impl StructuralEq for bool
impl StructuralPartialEq 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
impl<T> Any for T
where
T: 'static + ?Sized,
impl<T> Borrow<T> for T
where
T: ?Sized,
impl<T> BorrowMut<T> for T
where
T: ?Sized,
impl<T> From<T> for T
fn from(t: T) -> T
Returns the argument unchanged.
impl<T, U> Into<U> for T
where
U: From<T>,
fn into(self) -> U
Calls U::from(self)
.
That is, this conversion is whatever the implementation of From<T> for U
chooses to do.
impl<T> ToOwned for T
where
T: Clone,
type Owned = T
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)
impl<T> ToString for T
where
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T
where
U: Into<T>,
type Error = Infallible
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/primitive.bool.html