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

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

## Implementations

### `impl bool`[src]

#### `pub fn then_some<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_some(0), None);
    assert_eq!(true.then_some(0), Some(0));

Run

#### `pub fn then<T, F>(self, f: F) -> Option<T> where
F: FnOnce() -> T,` 1.50.0[src]

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

Run

## Trait Implementations

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

#### `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`[src]

Performs the `&` operation. Read more

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

#### `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`[src]

Performs the `&` operation. Read more

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

#### `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`[src]

Performs the `&` operation. Read more

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

#### `type Output = bool`

The resulting type after applying the `&` operator.

#### `pub fn bitand(self, rhs: bool) -> bool`[src]

Performs the `&` operation. Read more

### `impl<'_> BitAndAssign<&'_ bool> for bool` 1.22.0[src]

#### `pub fn bitand_assign(&mut self, other: &bool)`[src]

Performs the `&=` operation. Read more

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

#### `pub fn bitand_assign(&mut self, other: bool)`[src]

Performs the `&=` operation. Read more

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

#### `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`[src]

Performs the `|` operation. Read more

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

#### `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`[src]

Performs the `|` operation. Read more

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

#### `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`[src]

Performs the `|` operation. Read more

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

#### `type Output = bool`

The resulting type after applying the `|` operator.

#### `pub fn bitor(self, rhs: bool) -> bool`[src]

Performs the `|` operation. Read more

### `impl<'_> BitOrAssign<&'_ bool> for bool` 1.22.0[src]

#### `pub fn bitor_assign(&mut self, other: &bool)`[src]

Performs the `|=` operation. Read more

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

#### `pub fn bitor_assign(&mut self, other: bool)`[src]

Performs the `|=` operation. Read more

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

#### `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`[src]

Performs the `^` operation. Read more

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

#### `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`[src]

Performs the `^` operation. Read more

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

#### `type Output = bool`

The resulting type after applying the `^` operator.

#### `pub fn bitxor(self, other: bool) -> bool`[src]

Performs the `^` operation. Read more

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

#### `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`[src]

Performs the `^` operation. Read more

### `impl<'_> BitXorAssign<&'_ bool> for bool` 1.22.0[src]

#### `pub fn bitxor_assign(&mut self, other: &bool)`[src]

Performs the `^=` operation. Read more

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

#### `pub fn bitxor_assign(&mut self, other: bool)`[src]

Performs the `^=` operation. Read more

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

#### `pub fn clone(&self) -> bool`[src]

Returns a copy of the value. Read more

#### `pub fn clone_from(&mut self, source: &Self)`[src]

Performs copy-assignment from `source`. Read more

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

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

#### `pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>`[src]

Formats the value using the given formatter. Read more

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

#### `pub fn default() -> bool`[src]

Returns the default value of `false`

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

#### `pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>`[src]

Formats the value using the given formatter. Read more

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

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

#### `type Err = ParseBoolError`

The associated error which can be returned from parsing.

#### `pub 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 Hash for bool`[src]

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

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

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

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

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

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

The resulting type after applying the `!` operator.

#### `pub fn not(self) -> <bool as Not>::Output`[src]

Performs the unary `!` operation. Read more

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

#### `type Output = bool`

The resulting type after applying the `!` operator.

#### `pub fn not(self) -> bool`[src]

Performs the unary `!` operation. Read more

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

#### `pub fn cmp(&self, other: &bool) -> Ordering`[src]

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

#### `#[must_use]pub fn max(self, other: Self) -> Self` 1.21.0[src]

Compares and returns the maximum of two values. Read more

#### `#[must_use]pub fn min(self, other: Self) -> Self` 1.21.0[src]

Compares and returns the minimum of two values. Read more

#### `#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self` 1.50.0[src]

Restrict a value to a certain interval. Read more

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

#### `pub fn eq(&self, other: &bool) -> bool`[src]

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

#### `pub fn ne(&self, other: &bool) -> bool`[src]

This method tests for `!=`.

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

#### `pub fn partial_cmp(&self, other: &bool) -> Option<Ordering>`[src]

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

#### `#[must_use]pub fn lt(&self, other: &Rhs) -> bool`[src]

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

#### `#[must_use]pub fn le(&self, other: &Rhs) -> bool`[src]

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

#### `#[must_use]pub fn gt(&self, other: &Rhs) -> bool`[src]

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

#### `#[must_use]pub fn ge(&self, other: &Rhs) -> bool`[src]

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

## 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> Any for T where
T: 'static + ?Sized,` [src]

#### `pub fn type_id(&self) -> TypeId`[src]

Gets the `TypeId` of `self`. Read more

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

#### `pub fn borrow(&self) -> &Tⓘ

### Notable traits for &'_ mut F

`impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for &mut Rimpl<W: Write + ?Sized> Write for &mut W`
`[src]

Immutably borrows from an owned value. Read more

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

#### `pub fn borrow_mut(&mut self) -> &mut Tⓘ

### Notable traits for &'_ mut F

`impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<R: Read + ?Sized> Read for &mut Rimpl<W: Write + ?Sized> Write for &mut W`
`[src]

Mutably borrows from an owned value. Read more

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

#### `pub fn from(t: T) -> T`[src]

Performs the conversion.

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

#### `pub fn into(self) -> U`[src]

Performs the conversion.

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

#### `type Owned = T`

The resulting type after obtaining ownership.

#### `pub fn to_owned(&self) -> T`[src]

Creates owned data from borrowed data, usually by cloning. Read more

#### `pub fn clone_into(&self, target: &mut T)`[src]

🔬 This is a nightly-only experimental API. (`toowned_clone_into` #41263)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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

#### `pub default fn to_string(&self) -> String`[src]

Converts the given value to a `String`. Read more

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

#### `pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`[src]

Performs the conversion.

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

#### `pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`[src]

Performs the conversion.