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.

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).

Trait Implementations

impl BitXorAssign<bool> for bool
1.8.0
[src]

The method for the ^= operator

impl FromStr for bool
1.0.0
[src]

The associated error which can be returned from parsing.

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 BitOrAssign<bool> for bool
1.8.0
[src]

The method for the |= operator

impl Debug for bool
1.0.0
[src]

Formats the value using the given formatter.

impl Ord for bool
1.0.0
[src]

This method returns an Ordering between self and other. Read more

impl BitXor<bool> for bool
1.0.0
[src]

The resulting type after applying the ^ operator

The method for the ^ operator

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

The resulting type after applying the ^ operator

The method for the ^ operator

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

The resulting type after applying the ^ operator

The method for the ^ operator

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

The resulting type after applying the ^ operator

The method for the ^ operator

impl BitOr<bool> for bool
1.0.0
[src]

The resulting type after applying the | operator

The method for the | operator

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

The resulting type after applying the | operator

The method for the | operator

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

The resulting type after applying the | operator

The method for the | operator

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

The resulting type after applying the | operator

The method for the | operator

impl BitAnd<bool> for bool
1.0.0
[src]

The resulting type after applying the & operator

The method for the & operator

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

The resulting type after applying the & operator

The method for the & operator

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

The resulting type after applying the & operator

The method for the & operator

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

The resulting type after applying the & operator

The method for the & operator

impl BitAndAssign<bool> for bool
1.8.0
[src]

The method for the &= operator

impl PartialOrd<bool> for bool
1.0.0
[src]

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

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

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

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

impl PartialEq<bool> for bool
1.0.0
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Default for bool
1.0.0
[src]

Returns the "default value" for a type. Read more

impl Clone for bool
1.0.0
[src]

Returns a deep copy of the value.

Performs copy-assignment from source. Read more

impl Not for bool
1.0.0
[src]

The resulting type after applying the ! operator

The method for the unary ! operator

impl<'a> Not for &'a bool
1.0.0
[src]

The resulting type after applying the ! operator

The method for the unary ! operator

impl Hash for bool
1.0.0
[src]

Feeds this value into the state given, updating the hasher as necessary.

Feeds a slice of this type into the state provided.

impl Eq for bool
1.0.0
[src]

impl Display for bool
1.0.0
[src]

Formats the value using the given formatter.