1.0.0[]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 always demands a bool value. assert!, being an important macro in testing, checks whether an expression returns true.

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

Methods

impl bool[src]

pub fn then<T>(self, t: T) -> Option<T>[src]

🔬 This is a nightly-only experimental API. (bool_to_option #64260)

Returns Some(t) if the bool is true, or None otherwise.

Examples

#![feature(bool_to_option)]

assert_eq!(false.then(0), None);
assert_eq!(true.then(0), Some(0));Run

pub fn then_with<T, F>(self, f: F) -> Option<T> where
    F: FnOnce() -> T, 
[src]

🔬 This is a nightly-only experimental API. (bool_to_option #64260)

Returns Some(f()) if the bool is true, or None otherwise.

Examples

#![feature(bool_to_option)]

assert_eq!(false.then_with(|| 0), None);
assert_eq!(true.then_with(|| 0), Some(0));Run

Trait Implementations

impl BitOr<bool> for bool[src]

type Output = bool

The resulting type after applying the | operator.

impl<'_> BitOr<&'_ bool> for bool[src]

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

The resulting type after applying the | operator.

impl<'_, '_> BitOr<&'_ bool> for &'_ bool[src]

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

The resulting type after applying the | operator.

impl<'a> BitOr<bool> for &'a bool[src]

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

The resulting type after applying the | operator.

impl Display for bool[src]

impl<'_> BitAnd<&'_ bool> for bool[src]

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

The resulting type after applying the & operator.

impl BitAnd<bool> for bool[src]

type Output = bool

The resulting type after applying the & operator.

impl<'_, '_> BitAnd<&'_ bool> for &'_ bool[src]

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

The resulting type after applying the & operator.

impl<'a> BitAnd<bool> for &'a bool[src]

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

The resulting type after applying the & operator.

impl<'_> BitOrAssign<&'_ bool> for bool1.22.0[src]

impl BitOrAssign<bool> for bool1.8.0[src]

impl Eq for bool[src]

impl Debug for bool[src]

impl Not for bool[src]

type Output = bool

The resulting type after applying the ! operator.

impl<'_> Not for &'_ bool[src]

type Output = <bool as Not>::Output

The resulting type after applying the ! operator.

impl PartialEq<bool> for bool[src]

impl FromStr for bool[src]

type Err = ParseBoolError

The associated error which can be returned from parsing.

fn from_str(s: &str) -> Result<bool, ParseBoolError>[src]

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

impl PartialOrd<bool> for bool[src]

impl<'_> BitAndAssign<&'_ bool> for bool1.22.0[src]

impl BitAndAssign<bool> for bool1.8.0[src]

impl Hash for bool[src]

impl Ord for bool[src]

impl<'a> BitXor<bool> for &'a bool[src]

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

The resulting type after applying the ^ operator.

impl<'_, '_> BitXor<&'_ bool> for &'_ bool[src]

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

The resulting type after applying the ^ operator.

impl BitXor<bool> for bool[src]

type Output = bool

The resulting type after applying the ^ operator.

impl<'_> BitXor<&'_ bool> for bool[src]

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

The resulting type after applying the ^ operator.

impl Clone for bool[src]

impl<'_> BitXorAssign<&'_ bool> for bool1.22.0[src]

impl BitXorAssign<bool> for bool1.8.0[src]

impl Default for bool[src]

fn default() -> bool[src]

Returns the default value of false

impl Copy for bool[src]

Auto Trait Implementations

impl UnwindSafe for bool

impl RefUnwindSafe for bool

impl Unpin for bool

impl Send for bool

impl Sync for bool

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]