Primitive Type bool1.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
Trait Implementations
impl<'_> BitAndAssign<&'_ bool> for bool1.22.0[src]
impl<'_> BitAndAssign<&'_ bool> for bool1.22.0[src]pub fn bitand_assign(&mut self, other: &bool)[src]
pub fn bitand_assign(&mut self, other: &bool)[src]Performs the &= operation. Read more
impl BitAndAssign<bool> for bool1.8.0[src]
impl BitAndAssign<bool> for bool1.8.0[src]pub fn bitand_assign(&mut self, other: bool)[src]
pub fn bitand_assign(&mut self, other: bool)[src]Performs the &= operation. Read more
impl<'_> BitOrAssign<&'_ bool> for bool1.22.0[src]
impl<'_> BitOrAssign<&'_ bool> for bool1.22.0[src]pub fn bitor_assign(&mut self, other: &bool)[src]
pub fn bitor_assign(&mut self, other: &bool)[src]Performs the |= operation. Read more
impl BitOrAssign<bool> for bool1.8.0[src]
impl BitOrAssign<bool> for bool1.8.0[src]pub fn bitor_assign(&mut self, other: bool)[src]
pub fn bitor_assign(&mut self, other: bool)[src]Performs the |= operation. Read more
impl<'_> BitXorAssign<&'_ bool> for bool1.22.0[src]
impl<'_> BitXorAssign<&'_ bool> for bool1.22.0[src]pub fn bitxor_assign(&mut self, other: &bool)[src]
pub fn bitxor_assign(&mut self, other: &bool)[src]Performs the ^= operation. Read more
impl BitXorAssign<bool> for bool1.8.0[src]
impl BitXorAssign<bool> for bool1.8.0[src]pub fn bitxor_assign(&mut self, other: bool)[src]
pub fn bitxor_assign(&mut self, other: bool)[src]Performs the ^= operation. Read more
impl FromStr for bool[src]
impl FromStr for bool[src]pub fn from_str(s: &str) -> Result<bool, ParseBoolError>[src]
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
type Err = ParseBoolError
type Err = ParseBoolErrorThe associated error which can be returned from parsing.
impl Ord for bool[src]
impl Ord for bool[src]impl PartialOrd<bool> for bool[src]
impl PartialOrd<bool> for bool[src]pub fn partial_cmp(&self, other: &bool) -> Option<Ordering>[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]fn lt(&self, other: &Rhs) -> bool[src]
#[must_use]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]fn le(&self, other: &Rhs) -> bool[src]
#[must_use]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
impl Copy for bool[src]
impl Eq for bool[src]
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> Borrow<T> for T where
T: ?Sized, [src]
impl<T> Borrow<T> for T where
T: ?Sized, [src]pub fn borrow(&self) -> &TⓘNotable traits for &'_ mut I
impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for &mut Rimpl<W: Write + ?Sized> Write for &mut W[src]
pub fn borrow(&self) -> &TⓘNotable traits for &'_ mut I
impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;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]
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]pub fn borrow_mut(&mut self) -> &mut TⓘNotable traits for &'_ mut I
impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for &mut Rimpl<W: Write + ?Sized> Write for &mut W[src]
pub fn borrow_mut(&mut self) -> &mut TⓘNotable traits for &'_ mut I
impl<'_, I> Iterator for &'_ mut I where
I: Iterator + ?Sized, type Item = <I as Iterator>::Item;impl<'_, F> Future for &'_ mut F where
F: Future + Unpin + ?Sized, type Output = <F as Future>::Output;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> ToOwned for T where
T: Clone, [src]
impl<T> ToOwned for T where
T: Clone, [src]type Owned = T
type Owned = TThe resulting type after obtaining ownership.
pub fn to_owned(&self) -> T[src]
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]
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