W3cubDocs

/Rust

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

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!"),
}

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

Trait Implementations

impl Default for bool [src]

[src]

Returns the default value of false

impl PartialOrd<bool> for bool [src]

[src]

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

[src]

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

[src]

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

[src]

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

[src]

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

impl BitXorAssign<bool> for bool
1.8.0
[src]

[src]

Performs the ^= operation.

impl<'a> BitXorAssign<&'a bool> for bool
1.22.0
[src]

[src]

Performs the ^= operation.

impl BitAndAssign<bool> for bool
1.8.0
[src]

[src]

Performs the &= operation.

impl<'a> BitAndAssign<&'a bool> for bool
1.22.0
[src]

[src]

Performs the &= operation.

impl Eq for bool [src]

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

The resulting type after applying the | operator.

[src]

Performs the | operation.

impl BitOr<bool> for bool [src]

The resulting type after applying the | operator.

[src]

Performs the | operation.

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

The resulting type after applying the | operator.

[src]

Performs the | operation.

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

The resulting type after applying the | operator.

[src]

Performs the | operation.

impl Hash for bool [src]

[src]

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

1.3.0
[src]

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

impl Display for bool [src]

[src]

Formats the value using the given formatter. Read more

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

The resulting type after applying the & operator.

[src]

Performs the & operation.

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

The resulting type after applying the & operator.

[src]

Performs the & operation.

impl BitAnd<bool> for bool [src]

The resulting type after applying the & operator.

[src]

Performs the & operation.

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

The resulting type after applying the & operator.

[src]

Performs the & operation.

impl Ord for bool [src]

[src]

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

1.21.0
[src]

Compares and returns the maximum of two values. Read more

1.21.0
[src]

Compares and returns the minimum of two values. Read more

impl<'a> BitOrAssign<&'a bool> for bool
1.22.0
[src]

[src]

Performs the |= operation.

impl BitOrAssign<bool> for bool
1.8.0
[src]

[src]

Performs the |= operation.

impl PartialEq<bool> for bool [src]

[src]

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

[src]

This method tests for !=.

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

The resulting type after applying the ^ operator.

[src]

Performs the ^ operation.

impl BitXor<bool> for bool [src]

The resulting type after applying the ^ operator.

[src]

Performs the ^ operation.

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

The resulting type after applying the ^ operator.

[src]

Performs the ^ operation.

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

The resulting type after applying the ^ operator.

[src]

Performs the ^ operation.

impl Not for bool [src]

The resulting type after applying the ! operator.

[src]

Performs the unary ! operation.

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

The resulting type after applying the ! operator.

[src]

Performs the unary ! operation.

impl FromStr for bool [src]

The associated error which can be returned from parsing.

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

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

impl Debug for bool [src]

[src]

Formats the value using the given formatter. Read more

© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/primitive.bool.html