Primitive Type bool []

The boolean type.

The bool represents a value, which could only be either true or false.

Basic usage

bool implements various traits, such as BitAnd, BitOr, Not, etc., which allow us to perform boolean operations using &, | and !.

[if] always demands a bool value. assert!, being an important macro in testing, checks whether an expression returns true.

fn main() { let bool_val = true & false | false; assert!(!bool_val); }
let bool_val = true & false | false;
assert!(!bool_val);

Examples

A trivial example of the usage of bool,

fn main() { 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!"), } }
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).

Trait Implementations

impl Not for bool
1.0.0

type Output = bool

fn not(self) -> bool

impl<'a> Not for &'a bool
1.0.0

type Output = bool::Output

fn not(self) -> bool::Output

impl BitAnd<bool> for bool
1.0.0

type Output = bool

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

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

type Output = bool::Output

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

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

type Output = bool::Output

fn bitand(self, other: &'a bool) -> bool::Output

impl<'a, 'b> BitAnd<&'a bool> for &'b bool
1.0.0

type Output = bool::Output

fn bitand(self, other: &'a bool) -> bool::Output

impl BitOr<bool> for bool
1.0.0

type Output = bool

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

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

type Output = bool::Output

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

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

type Output = bool::Output

fn bitor(self, other: &'a bool) -> bool::Output

impl<'a, 'b> BitOr<&'a bool> for &'b bool
1.0.0

type Output = bool::Output

fn bitor(self, other: &'a bool) -> bool::Output

impl BitXor<bool> for bool
1.0.0

type Output = bool

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

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

type Output = bool::Output

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

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

type Output = bool::Output

fn bitxor(self, other: &'a bool) -> bool::Output

impl<'a, 'b> BitXor<&'a bool> for &'b bool
1.0.0

type Output = bool::Output

fn bitxor(self, other: &'a bool) -> bool::Output

impl BitAndAssign<bool> for bool
1.8.0

fn bitand_assign(&mut self, other: bool)

impl BitOrAssign<bool> for bool
1.8.0

fn bitor_assign(&mut self, other: bool)

impl BitXorAssign<bool> for bool
1.8.0

fn bitxor_assign(&mut self, other: bool)

impl PartialEq<bool> for bool
1.0.0

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

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

impl Eq for bool
1.0.0

impl PartialOrd<bool> for bool
1.0.0

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

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

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

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

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

impl Ord for bool
1.0.0

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

impl Clone for bool
1.0.0

fn clone(&self) -> bool

Returns a deep copy of the value.

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

impl Default for bool
1.0.0

fn default() -> bool

impl FromStr for bool
1.0.0

type Err = ParseBoolError

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

fn main() { 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()); }
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.

fn main() { assert_eq!("true".parse(), Ok(true)); assert_eq!("false".parse(), Ok(false)); assert!("not even a boolean".parse::<bool>().is_err()); }
assert_eq!("true".parse(), Ok(true));
assert_eq!("false".parse(), Ok(false));
assert!("not even a boolean".parse::<bool>().is_err());

impl Hash for bool
1.0.0

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

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

impl Debug for bool
1.0.0

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

impl Display for bool
1.0.0

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