bool - Rust
Description: API documentation for the Rust `bool` primitive in crate `std`.
Keywords: rust, rustlang, rust-lang, bool

# Primitive Type bool 1.0.0 [−]

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

## Trait Implementations

### `impl BitXorAssign<bool> for bool`
1.8.0
[src]

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

The method for the `^=` operator

### `impl Hash for bool`
[src]

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

Feeds this value into the given [`Hasher`]. Read more

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

Feeds a slice of this type into the given [`Hasher`]. Read more

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

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

The resulting type after applying the `^` operator

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

The method for the `^` operator

### `impl BitXor<bool> for bool`
[src]

#### `type Output = bool`

The resulting type after applying the `^` operator

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

The method for the `^` operator

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

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

The resulting type after applying the `^` operator

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

The method for the `^` operator

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

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

The resulting type after applying the `^` operator

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

The method for the `^` operator

### `impl Eq 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>`

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<'a> BitOr<&'a bool> for bool`
[src]

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

The resulting type after applying the `|` operator

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

The method for the `|` operator

### `impl BitOr<bool> for bool`
[src]

#### `type Output = bool`

The resulting type after applying the `|` operator

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

The method for the `|` operator

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

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

The resulting type after applying the `|` operator

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

The method for the `|` operator

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

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

The resulting type after applying the `|` operator

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

The method for the `|` operator

### `impl BitAndAssign<bool> for bool`
1.8.0
[src]

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

The method for the `&=` operator

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

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

The resulting type after applying the `&` operator

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

The method for the `&` operator

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

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

The resulting type after applying the `&` operator

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

The method for the `&` operator

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

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

The resulting type after applying the `&` operator

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

The method for the `&` operator

### `impl BitAnd<bool> for bool`
[src]

#### `type Output = bool`

The resulting type after applying the `&` operator

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

The method for the `&` operator

### `impl Display for bool`
[src]

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

Formats the value using the given formatter. Read more

### `impl BitOrAssign<bool> for bool`
1.8.0
[src]

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

The method for the `|=` operator

### `impl Clone for bool`
[src]

#### `fn clone(&self) -> bool`

Returns a deep copy of the value.

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

Performs copy-assignment from `source`. Read more

### `impl Default for bool`
[src]

#### `fn default() -> bool`

Returns the default value of `false`

### `impl Debug for bool`
[src]

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

Formats the value using the given formatter.

### `impl Not for bool`
[src]

#### `type Output = bool`

The resulting type after applying the `!` operator

#### `fn not(self) -> bool`

The method for the unary `!` operator

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

#### `type Output = <bool as Not>::Output`

The resulting type after applying the `!` operator

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

The method for the unary `!` operator

### `impl PartialEq<bool> for bool`
[src]

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

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

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

This method tests for `!=`.

### `impl Ord for bool`
[src]

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

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

#### `fn max(self, other: Self) -> Self`

🔬 This is a nightly-only experimental API. (`ord_max_min` #25663)

Compares and returns the maximum of two values. Read more

#### `fn min(self, other: Self) -> Self`

🔬 This is a nightly-only experimental API. (`ord_max_min` #25663)

Compares and returns the minimum of two values. Read more

### `impl PartialOrd<bool> for bool`
[src]

#### `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

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)