Primitive Type bool
Expand description
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.
§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:
Implementations§
Source§impl bool
impl bool
1.62.0 (const: unstable) · Sourcepub fn then_some<T>(self, t: T) -> Option<T>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
pub fn then_some<T>(self, t: T) -> Option<T>
1.50.0 (const: unstable) · Sourcepub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T>
Sourcepub const fn ok_or<E>(self, err: E) -> Result<(), E>
🔬This is a nightly-only experimental API. (bool_to_result #142748)This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
pub const fn ok_or<E>(self, err: E) -> Result<(), E>
bool_to_result #142748)Returns Ok(()) if the bool is true,
or Err(err) otherwise.
Arguments passed to ok_or are eagerly evaluated; if you are
passing the result of a function call, it is recommended to use
ok_or_else, which is lazily evaluated.
§Examples
#![feature(bool_to_result)]
let mut a = 0;
let mut function_with_side_effects = || { a += 1; };
assert!(true.ok_or(function_with_side_effects()).is_ok());
assert!(false.ok_or(function_with_side_effects()).is_err());
// `a` is incremented twice because the value passed to `ok_or` is
// evaluated eagerly.
assert_eq!(a, 2);Sourcepub const fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E>
🔬This is a nightly-only experimental API. (bool_to_result #142748)This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
pub const fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E>
bool_to_result #142748)Trait Implementations§
Source§impl AtomicPrimitive for bool
impl AtomicPrimitive for bool
1.22.0 (const: unstable) · Source§impl BitAndAssign<&bool> for bool
impl BitAndAssign<&bool> for bool
Source§fn bitand_assign(&mut self, other: &bool)
fn bitand_assign(&mut self, other: &bool)
&= operation. Read moreSource§impl<T, const N: usize> BitAndAssign<bool> for Mask<T, N>where
T: MaskElement,
impl<T, const N: usize> BitAndAssign<bool> for Mask<T, N>where
T: MaskElement,
Source§fn bitand_assign(&mut self, rhs: bool)
fn bitand_assign(&mut self, rhs: bool)
&= operation. Read more1.8.0 (const: unstable) · Source§impl BitAndAssign for bool
impl BitAndAssign for bool
Source§fn bitand_assign(&mut self, other: bool)
fn bitand_assign(&mut self, other: bool)
&= operation. Read more1.22.0 (const: unstable) · Source§impl BitOrAssign<&bool> for bool
impl BitOrAssign<&bool> for bool
Source§fn bitor_assign(&mut self, other: &bool)
fn bitor_assign(&mut self, other: &bool)
|= operation. Read moreSource§impl<T, const N: usize> BitOrAssign<bool> for Mask<T, N>where
T: MaskElement,
impl<T, const N: usize> BitOrAssign<bool> for Mask<T, N>where
T: MaskElement,
Source§fn bitor_assign(&mut self, rhs: bool)
fn bitor_assign(&mut self, rhs: bool)
|= operation. Read more1.8.0 (const: unstable) · Source§impl BitOrAssign for bool
impl BitOrAssign for bool
Source§fn bitor_assign(&mut self, other: bool)
fn bitor_assign(&mut self, other: bool)
|= operation. Read more1.22.0 (const: unstable) · Source§impl BitXorAssign<&bool> for bool
impl BitXorAssign<&bool> for bool
Source§fn bitxor_assign(&mut self, other: &bool)
fn bitxor_assign(&mut self, other: &bool)
^= operation. Read moreSource§impl<T, const N: usize> BitXorAssign<bool> for Mask<T, N>where
T: MaskElement,
impl<T, const N: usize> BitXorAssign<bool> for Mask<T, N>where
T: MaskElement,
Source§fn bitxor_assign(&mut self, rhs: bool)
fn bitxor_assign(&mut self, rhs: bool)
^= operation. Read more1.8.0 (const: unstable) · Source§impl BitXorAssign for bool
impl BitXorAssign for bool
Source§fn bitxor_assign(&mut self, other: bool)
fn bitxor_assign(&mut self, other: bool)
^= operation. Read more1.0.0 (const: unstable) · Source§impl Clone for bool
impl Clone for bool
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
1.0.0 · Source§fn clone_from(&mut self, source: &Self)where
Self:,
fn clone_from(&mut self, source: &Self)where
Self:,
source. Read moreSource§impl DisjointBitOr for bool
impl DisjointBitOr for bool
Source§unsafe fn disjoint_bitor(self, other: Self) -> Self
unsafe fn disjoint_bitor(self, other: Self) -> Self
core_intrinsics_fallbacks)super::disjoint_bitor; we just need the trait indirection to handle
different types since calling intrinsics with generics doesn’t work.Source§impl Distribution<bool> for RangeFull
impl Distribution<bool> for RangeFull
1.68.0 (const: unstable) · Source§impl From<bool> for f128
impl From<bool> for f128
1.68.0 (const: unstable) · Source§impl From<bool> for f16
impl From<bool> for f16
1.68.0 (const: unstable) · Source§impl From<bool> for f32
impl From<bool> for f32
1.68.0 (const: unstable) · Source§impl From<bool> for f64
impl From<bool> for f64
1.0.0 · Source§impl FromStr for bool
impl FromStr for bool
Source§fn from_str(s: &str) -> Result<bool, ParseBoolError>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn from_str(s: &str) -> Result<bool, ParseBoolError>
Parse a bool from a string.
The only accepted values are "true" and "false". Any other input
will return an error.
§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.
Source§type Err = ParseBoolError
type Err = ParseBoolError
1.0.0 · Source§impl Hash for bool
impl Hash for bool
1.0.0 (const: unstable) · Source§impl Ord for bool
impl Ord for bool
Source§fn cmp(&self, other: &bool) -> Ordering
fn cmp(&self, other: &bool) -> Ordering
Source§fn min(self, other: bool) -> bool
fn min(self, other: bool) -> bool
1.0.0 (const: unstable) · Source§impl PartialEq for bool
impl PartialEq for bool
1.0.0 (const: unstable) · Source§impl PartialOrd for bool
impl PartialOrd for bool
Source§fn partial_cmp(&self, other: &bool) -> Option<Ordering>
fn partial_cmp(&self, other: &bool) -> Option<Ordering>
Source§fn lt(&self, other: &Self) -> bool
fn lt(&self, other: &Self) -> bool
Source§fn le(&self, other: &Self) -> bool
fn le(&self, other: &Self) -> bool
1.34.0 (const: unstable) · Source§impl TryFrom<i128> for bool
impl TryFrom<i128> for bool
Source§fn try_from(i: i128) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: i128) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.
§Examples
Source§type Error = TryFromIntError
type Error = TryFromIntError
1.34.0 (const: unstable) · Source§impl TryFrom<i16> for bool
impl TryFrom<i16> for bool
Source§fn try_from(i: i16) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: i16) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.
§Examples
Source§type Error = TryFromIntError
type Error = TryFromIntError
1.34.0 (const: unstable) · Source§impl TryFrom<i32> for bool
impl TryFrom<i32> for bool
Source§fn try_from(i: i32) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: i32) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.
§Examples
Source§type Error = TryFromIntError
type Error = TryFromIntError
1.34.0 (const: unstable) · Source§impl TryFrom<i64> for bool
impl TryFrom<i64> for bool
Source§fn try_from(i: i64) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: i64) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.
§Examples
Source§type Error = TryFromIntError
type Error = TryFromIntError
1.34.0 (const: unstable) · Source§impl TryFrom<i8> for bool
impl TryFrom<i8> for bool
Source§fn try_from(i: i8) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: i8) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.
§Examples
Source§type Error = TryFromIntError
type Error = TryFromIntError
1.34.0 (const: unstable) · Source§impl TryFrom<u128> for bool
impl TryFrom<u128> for bool
Source§fn try_from(i: u128) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: u128) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.
§Examples
Source§type Error = TryFromIntError
type Error = TryFromIntError
1.34.0 (const: unstable) · Source§impl TryFrom<u16> for bool
impl TryFrom<u16> for bool
Source§fn try_from(i: u16) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: u16) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.
§Examples
Source§type Error = TryFromIntError
type Error = TryFromIntError
1.34.0 (const: unstable) · Source§impl TryFrom<u32> for bool
impl TryFrom<u32> for bool
Source§fn try_from(i: u32) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: u32) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.
§Examples
Source§type Error = TryFromIntError
type Error = TryFromIntError
1.34.0 (const: unstable) · Source§impl TryFrom<u64> for bool
impl TryFrom<u64> for bool
Source§fn try_from(i: u64) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: u64) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.
§Examples
Source§type Error = TryFromIntError
type Error = TryFromIntError
1.34.0 (const: unstable) · Source§impl TryFrom<u8> for bool
impl TryFrom<u8> for bool
Source§fn try_from(i: u8) -> Result<Self, Self::Error>
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).
fn try_from(i: u8) -> Result<Self, Self::Error>
Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.