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.
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);
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);
impl bool
[src]
pub fn then_some<T>(self, t: T) -> Option<T>
[src]
Returns Some(t)
if the bool
is true
, or None
otherwise.
#![feature(bool_to_option)] assert_eq!(false.then_some(0), None); assert_eq!(true.then_some(0), Some(0));
pub fn then<T, F>(self, f: F) -> Option<T> where
    F: FnOnce() -> T,Â
[src]
Returns Some(f())
if the bool
is true
, or None
otherwise.
#![feature(bool_to_option)] assert_eq!(false.then(|| 0), None); assert_eq!(true.then(|| 0), Some(0));
impl<'_> BitAnd<&'_ bool> for 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
[src]
impl<'_, '_> BitAnd<&'_ bool> for &'_ 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
[src]
impl BitAnd<bool> for bool
[src]
type Output = bool
The resulting type after applying the &
operator.
fn bitand(self, rhs: bool) -> bool
[src]
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
[src]
impl<'_> BitAndAssign<&'_ bool> for bool
[src]1.22.0
fn bitand_assign(&mut self, other: &bool)
[src]
impl BitAndAssign<bool> for bool
[src]1.8.0
fn bitand_assign(&mut self, other: bool)
[src]
impl<'_> BitOr<&'_ bool> for 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
[src]
impl<'_, '_> BitOr<&'_ bool> for &'_ 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
[src]
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
[src]
impl BitOr<bool> for bool
[src]
type Output = bool
The resulting type after applying the |
operator.
fn bitor(self, rhs: bool) -> bool
[src]
impl<'_> BitOrAssign<&'_ bool> for bool
[src]1.22.0
fn bitor_assign(&mut self, other: &bool)
[src]
impl BitOrAssign<bool> for bool
[src]1.8.0
fn bitor_assign(&mut self, other: bool)
[src]
impl<'_, '_> BitXor<&'_ bool> for &'_ 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
[src]
impl<'_> BitXor<&'_ bool> for 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
[src]
impl BitXor<bool> for bool
[src]
type Output = bool
The resulting type after applying the ^
operator.
fn bitxor(self, other: bool) -> bool
[src]
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
[src]
impl<'_> BitXorAssign<&'_ bool> for bool
[src]1.22.0
fn bitxor_assign(&mut self, other: &bool)
[src]
impl BitXorAssign<bool> for bool
[src]1.8.0
fn bitxor_assign(&mut self, other: bool)
[src]
impl Clone for bool
[src]
impl Copy for bool
[src]
impl Debug for bool
[src]
impl Default for bool
[src]
impl Display for bool
[src]
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>
[src]
Parse a bool
from a string.
Yields a Result<bool, ParseBoolError>
, because s
may or may not actually be parseable.
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 Hash for bool
[src]
fn hash<H>(&self, state: &mut H) where
    H: Hasher,Â
[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher,Â
[src]1.3.0
impl Not for bool
[src]
impl<'_> Not for &'_ bool
[src]
type Output = <bool as Not>::Output
The resulting type after applying the !
operator.
fn not(self) -> <bool as Not>::Output
[src]
impl Ord for bool
[src]
fn cmp(&self, other: &bool) -> Ordering
[src]
fn max(self, other: Self) -> Self
[src]1.21.0
fn min(self, other: Self) -> Self
[src]1.21.0
fn clamp(self, min: Self, max: Self) -> Self
[src]
impl PartialEq<bool> for bool
[src]
impl PartialOrd<bool> for bool
[src]
impl RefUnwindSafe for bool
impl Send for bool
impl Sync for bool
impl Unpin for bool
impl UnwindSafe for bool
impl<T> Any for T where
    T: 'static + ?Sized,Â
[src]
impl<T> Borrow<T> for T where
    T: ?Sized,Â
[src]
fn borrow(&self) -> &TⓘNotable traits for &'_ mut F
impl<'_, F> Future for &'_ mut F where
    F: Unpin + Future + ?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 R
impl<W:Â Write + ?Sized, '_> Write for &'_ mut W
[src]
impl<T> BorrowMut<T> for T where
    T: ?Sized,Â
[src]
fn borrow_mut(&mut self) -> &mut TⓘNotable traits for &'_ mut F
impl<'_, F> Future for &'_ mut F where
    F: Unpin + Future + ?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 R
impl<W:Â Write + ?Sized, '_> Write for &'_ mut W
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
    U: From<T>,Â
[src]
impl<T> ToOwned for T where
    T: Clone,Â
[src]
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
    T: Display + ?Sized,Â
[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.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
    U: TryFrom<T>,Â
[src]
© 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