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.
Examples
#![feature(bool_to_option)] assert_eq!(false.then_some(0), None); assert_eq!(true.then_some(0), Some(0));
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));
Trait Implementations
impl<'_> BitAnd<&'_ bool> for bool
type Output = <bool as BitAnd<bool>>::Output
The resulting type after applying the &
operator.
pub fn bitand(self, other: &bool) -> <bool as BitAnd<bool>>::Output
Performs the &
operation. Read more
impl<'_, '_> BitAnd<&'_ bool> for &'_ bool
type Output = <bool as BitAnd<bool>>::Output
The resulting type after applying the &
operator.
pub fn bitand(self, other: &bool) -> <bool as BitAnd<bool>>::Output
Performs the &
operation. Read more
impl<'a> BitAnd<bool> for &'a bool
type Output = <bool as BitAnd<bool>>::Output
The resulting type after applying the &
operator.
pub fn bitand(self, other: bool) -> <bool as BitAnd<bool>>::Output
Performs the &
operation. Read more
impl BitAnd<bool> for bool
type Output = bool
The resulting type after applying the &
operator.
pub fn bitand(self, rhs: bool) -> bool
Performs the &
operation. Read more
impl<'_> BitAndAssign<&'_ bool> for bool
impl BitAndAssign<bool> for bool
impl<'_> BitOr<&'_ bool> for bool
type Output = <bool as BitOr<bool>>::Output
The resulting type after applying the |
operator.
pub fn bitor(self, other: &bool) -> <bool as BitOr<bool>>::Output
Performs the |
operation. Read more
impl<'_, '_> BitOr<&'_ bool> for &'_ bool
type Output = <bool as BitOr<bool>>::Output
The resulting type after applying the |
operator.
pub fn bitor(self, other: &bool) -> <bool as BitOr<bool>>::Output
Performs the |
operation. Read more
impl<'a> BitOr<bool> for &'a bool
type Output = <bool as BitOr<bool>>::Output
The resulting type after applying the |
operator.
pub fn bitor(self, other: bool) -> <bool as BitOr<bool>>::Output
Performs the |
operation. Read more
impl BitOr<bool> for bool
type Output = bool
The resulting type after applying the |
operator.
pub fn bitor(self, rhs: bool) -> bool
Performs the |
operation. Read more
impl<'_> BitOrAssign<&'_ bool> for bool
impl BitOrAssign<bool> for bool
impl<'_> BitXor<&'_ bool> for bool
type Output = <bool as BitXor<bool>>::Output
The resulting type after applying the ^
operator.
pub fn bitxor(self, other: &bool) -> <bool as BitXor<bool>>::Output
Performs the ^
operation. Read more
impl<'_, '_> BitXor<&'_ bool> for &'_ bool
type Output = <bool as BitXor<bool>>::Output
The resulting type after applying the ^
operator.
pub fn bitxor(self, other: &bool) -> <bool as BitXor<bool>>::Output
Performs the ^
operation. Read more
impl<'a> BitXor<bool> for &'a bool
type Output = <bool as BitXor<bool>>::Output
The resulting type after applying the ^
operator.
pub fn bitxor(self, other: bool) -> <bool as BitXor<bool>>::Output
Performs the ^
operation. Read more
impl BitXor<bool> for bool
type Output = bool
The resulting type after applying the ^
operator.
pub fn bitxor(self, other: bool) -> bool
Performs the ^
operation. Read more
impl<'_> BitXorAssign<&'_ bool> for bool
impl BitXorAssign<bool> for bool
impl Clone for bool
pub fn clone(&self) -> bool
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Debug for bool
pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Formats the value using the given formatter. Read more
pub fn default() -> bool
Returns the default value of false
impl Display for bool
pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Formats the value using the given formatter. Read more
impl FromStr for bool
pub 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());
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
The associated error which can be returned from parsing.
impl Hash for bool
impl Not for bool
type Output = bool
The resulting type after applying the !
operator.
pub fn not(self) -> bool
Performs the unary !
operation. Read more
impl<'_> Not for &'_ bool
type Output = <bool as Not>::Output
The resulting type after applying the !
operator.
pub fn not(self) -> <bool as Not>::Output
Performs the unary !
operation. Read more
impl Ord for bool
pub fn cmp(&self, other: &bool) -> Ordering
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
fn clamp(self, min: Self, max: Self) -> Self
Restrict a value to a certain interval. Read more
impl PartialEq<bool> for bool
pub fn eq(&self, other: &bool) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
pub fn ne(&self, other: &bool) -> bool
This method tests for !=
.
impl PartialOrd<bool> for bool
pub fn partial_cmp(&self, other: &bool) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
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
fn gt(&self, other: &Rhs) -> bool
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
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
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
impl<T> From<T> for T
pub fn from(t: T) -> T
Performs the conversion.
pub fn into(self) -> U
Performs the conversion.
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
#41263)recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
Performs the conversion.
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
Performs the conversion.
© 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