Primitive Type bool

1.0.0 ·
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);
Run

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!"),
}
Run

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);
Run

Implementations§

source§

impl bool

1.62.0 (const: unstable) · source

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));
Run
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);
Run
1.50.0 (const: unstable) · source

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));
Run
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);
Run

Trait Implementations§

const: unstable · source§

impl BitAnd<&bool> for &bool

§

type Output = <bool as BitAnd<bool>>::Output

The resulting type after applying the & operator.
const: unstable · source§

fn bitand(self, other: &bool) -> <bool as BitAnd<bool>>::Output

Performs the & operation. Read more
const: unstable · source§

impl BitAnd<&bool> for bool

§

type Output = <bool as BitAnd<bool>>::Output

The resulting type after applying the & operator.
const: unstable · source§

fn bitand(self, other: &bool) -> <bool as BitAnd<bool>>::Output

Performs the & operation. Read more
source§

impl<T, const LANES: usize> BitAnd<Mask<T, LANES>> for boolwhere T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

§

type Output = Mask<T, LANES>

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: Mask<T, LANES>) -> Mask<T, LANES>

Performs the & operation. Read more
const: unstable · source§

impl<'a> BitAnd<bool> for &'a bool

§

type Output = <bool as BitAnd<bool>>::Output

The resulting type after applying the & operator.
const: unstable · source§

fn bitand(self, other: bool) -> <bool as BitAnd<bool>>::Output

Performs the & operation. Read more
source§

impl<T, const LANES: usize> BitAnd<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

§

type Output = Mask<T, LANES>

The resulting type after applying the & operator.
source§

fn bitand(self, rhs: bool) -> Self

Performs the & operation. Read more
const: unstable · source§

impl BitAnd<bool> for bool

§

type Output = bool

The resulting type after applying the & operator.
const: unstable · source§

fn bitand(self, rhs: bool) -> bool

Performs the & operation. Read more
1.22.0 (const: unstable) · source§

impl BitAndAssign<&bool> for bool

const: unstable · source§

fn bitand_assign(&mut self, other: &bool)

Performs the &= operation. Read more
source§

impl<T, const LANES: usize> BitAndAssign<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

source§

fn bitand_assign(&mut self, rhs: bool)

Performs the &= operation. Read more
1.8.0 (const: unstable) · source§

impl BitAndAssign<bool> for bool

const: unstable · source§

fn bitand_assign(&mut self, other: bool)

Performs the &= operation. Read more
const: unstable · source§

impl BitOr<&bool> for &bool

§

type Output = <bool as BitOr<bool>>::Output

The resulting type after applying the | operator.
const: unstable · source§

fn bitor(self, other: &bool) -> <bool as BitOr<bool>>::Output

Performs the | operation. Read more
const: unstable · source§

impl BitOr<&bool> for bool

§

type Output = <bool as BitOr<bool>>::Output

The resulting type after applying the | operator.
const: unstable · source§

fn bitor(self, other: &bool) -> <bool as BitOr<bool>>::Output

Performs the | operation. Read more
source§

impl<T, const LANES: usize> BitOr<Mask<T, LANES>> for boolwhere T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

§

type Output = Mask<T, LANES>

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: Mask<T, LANES>) -> Mask<T, LANES>

Performs the | operation. Read more
const: unstable · source§

impl<'a> BitOr<bool> for &'a bool

§

type Output = <bool as BitOr<bool>>::Output

The resulting type after applying the | operator.
const: unstable · source§

fn bitor(self, other: bool) -> <bool as BitOr<bool>>::Output

Performs the | operation. Read more
source§

impl<T, const LANES: usize> BitOr<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

§

type Output = Mask<T, LANES>

The resulting type after applying the | operator.
source§

fn bitor(self, rhs: bool) -> Self

Performs the | operation. Read more
const: unstable · source§

impl BitOr<bool> for bool

§

type Output = bool

The resulting type after applying the | operator.
const: unstable · source§

fn bitor(self, rhs: bool) -> bool

Performs the | operation. Read more
1.22.0 (const: unstable) · source§

impl BitOrAssign<&bool> for bool

const: unstable · source§

fn bitor_assign(&mut self, other: &bool)

Performs the |= operation. Read more
source§

impl<T, const LANES: usize> BitOrAssign<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

source§

fn bitor_assign(&mut self, rhs: bool)

Performs the |= operation. Read more
1.8.0 (const: unstable) · source§

impl BitOrAssign<bool> for bool

const: unstable · source§

fn bitor_assign(&mut self, other: bool)

Performs the |= operation. Read more
const: unstable · source§

impl BitXor<&bool> for &bool

§

type Output = <bool as BitXor<bool>>::Output

The resulting type after applying the ^ operator.
const: unstable · source§

fn bitxor(self, other: &bool) -> <bool as BitXor<bool>>::Output

Performs the ^ operation. Read more
const: unstable · source§

impl BitXor<&bool> for bool

§

type Output = <bool as BitXor<bool>>::Output

The resulting type after applying the ^ operator.
const: unstable · source§

fn bitxor(self, other: &bool) -> <bool as BitXor<bool>>::Output

Performs the ^ operation. Read more
source§

impl<T, const LANES: usize> BitXor<Mask<T, LANES>> for boolwhere T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

§

type Output = Mask<T, LANES>

The resulting type after applying the ^ operator.
source§

fn bitxor(self, rhs: Mask<T, LANES>) -> Self::Output

Performs the ^ operation. Read more
const: unstable · source§

impl<'a> BitXor<bool> for &'a bool

§

type Output = <bool as BitXor<bool>>::Output

The resulting type after applying the ^ operator.
const: unstable · source§

fn bitxor(self, other: bool) -> <bool as BitXor<bool>>::Output

Performs the ^ operation. Read more
source§

impl<T, const LANES: usize> BitXor<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

§

type Output = Mask<T, LANES>

The resulting type after applying the ^ operator.
source§

fn bitxor(self, rhs: bool) -> Self::Output

Performs the ^ operation. Read more
const: unstable · source§

impl BitXor<bool> for bool

§

type Output = bool

The resulting type after applying the ^ operator.
const: unstable · source§

fn bitxor(self, other: bool) -> bool

Performs the ^ operation. Read more
1.22.0 (const: unstable) · source§

impl BitXorAssign<&bool> for bool

const: unstable · source§

fn bitxor_assign(&mut self, other: &bool)

Performs the ^= operation. Read more
source§

impl<T, const LANES: usize> BitXorAssign<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,

source§

fn bitxor_assign(&mut self, rhs: bool)

Performs the ^= operation. Read more
1.8.0 (const: unstable) · source§

impl BitXorAssign<bool> for bool

const: unstable · source§

fn bitxor_assign(&mut self, other: bool)

Performs the ^= operation. Read more
const: unstable · source§

impl Clone for bool

const: unstable · source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for bool

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
const: unstable · source§

impl Default for bool

const: unstable · source§

fn default() -> bool

Returns the default value of false

source§

impl Display for bool

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
1.24.0 (const: unstable) · source§

impl From<bool> for AtomicBool

const: unstable · source§

fn from(b: bool) -> Self

Converts a bool into an AtomicBool.

Examples
use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{atomic_bool:?}"), "true")
Run
1.68.0 (const: unstable) · source§

impl From<bool> for f32

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.68.0 (const: unstable) · source§

impl From<bool> for f64

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for i128

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for i16

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for i32

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for i64

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for i8

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for isize

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for u128

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for u16

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for u32

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for u64

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for u8

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
1.28.0 (const: unstable) · source§

impl From<bool> for usize

const: unstable · source§

fn from(small: bool) -> Self

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);
Run
source§

impl FromStr for bool

source§

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());
Run

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());
Run
§

type Err = ParseBoolError

The associated error which can be returned from parsing.
const: unstable · source§

impl Hash for bool

const: unstable · source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)where Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
const: unstable · source§

impl Not for &bool

§

type Output = <bool as Not>::Output

The resulting type after applying the ! operator.
const: unstable · source§

fn not(self) -> <bool as Not>::Output

Performs the unary ! operation. Read more
const: unstable · source§

impl Not for bool

§

type Output = bool

The resulting type after applying the ! operator.
const: unstable · source§

fn not(self) -> bool

Performs the unary ! operation. Read more
const: unstable · source§

impl Ord for bool

const: unstable · source§

fn cmp(&self, other: &bool) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
const: unstable · source§

impl PartialEq<bool> for bool

const: unstable · source§

fn eq(&self, other: &bool) -> bool

This method tests for self and other values to be equal, and is used by ==.
const: unstable · source§

fn ne(&self, other: &bool) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
const: unstable · source§

impl PartialOrd<bool> for bool

const: unstable · source§

fn partial_cmp(&self, other: &bool) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
source§

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
source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Copy for bool

source§

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.