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>
pub fn then_some<T>(self, t: T) -> Option<T>
1.50.0 (const: unstable) · Sourcepub fn then<T, F>(self, f: F) -> Option<T>where
F: FnOnce() -> T,
pub fn then<T, F>(self, f: F) -> Option<T>where
F: FnOnce() -> T,
1.98.0 (const: unstable) · Sourcepub fn ok_or<E>(self, err: E) -> Result<(), E>
pub fn ok_or<E>(self, err: E) -> Result<(), E>
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
1.98.0 (const: unstable) · Sourcepub fn ok_or_else<E, F>(self, f: F) -> Result<(), E>where
F: FnOnce() -> E,
pub fn ok_or_else<E, F>(self, f: F) -> Result<(), E>where
F: FnOnce() -> E,
Sourcepub const unsafe fn unchecked_disjoint_bitor(self, rhs: bool) -> bool
🔬This is a nightly-only experimental API. (disjoint_bitor #135758)
pub const unsafe fn unchecked_disjoint_bitor(self, rhs: bool) -> bool
disjoint_bitor #135758)Disjoint, bitwise or. Computes self | rhs, assuming at most one, true value.
Practically, this requires that self | rhs and self ^ rhs both yield the
same result, allowing for any of the two to be emitted in codegen – depending
on whichever is cheapest.
§Examples
#![feature(disjoint_bitor)]
assert_eq!(
// SAFETY: Only one, true value is passed.
unsafe { false.unchecked_disjoint_bitor(true) },
true,
);
assert_eq!(
// SAFETY: No true values are passed.
unsafe { false.unchecked_disjoint_bitor(false) },
false,
);§Safety
This results in undefined behaviour if both self and rhs are true.
Trait Implementations§
Source§impl AtomicPrimitive for bool
Available on target_has_atomic_load_store=8 only.
impl AtomicPrimitive for bool
target_has_atomic_load_store=8 only.1.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 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 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 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 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.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 moreimpl ConstParamTy_ for bool
impl Copy for bool
Source§impl DisjointBitOr for bool
impl DisjointBitOr for bool
Source§impl Distribution<bool> for RangeFull
impl Distribution<bool> for RangeFull
1.0.0 (const: unstable) · Source§impl Eq for bool
impl Eq for bool
1.0.0 (const: unstable) · Source§#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
implementation detail of #[derive(Eq)]
Source§#[doc(hidden)]fn assert_fields_are_eq(&self)
#[doc(hidden)]fn assert_fields_are_eq(&self)
derive_eq_internals)1.24.0 (const: unstable) · Source§impl From<bool> for Atomic<bool>
Available on target_has_atomic_load_store=8 only.
impl From<bool> for Atomic<bool>
target_has_atomic_load_store=8 only.1.0.0 · Source§impl FromStr for bool
impl FromStr for bool
Source§fn from_str(s: &str) -> Result<bool, ParseBoolError>
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 (const: unstable) · Source§impl Ord for bool
impl Ord for bool
1.0.0 (const: unstable) · Source§impl PartialOrd for bool
impl PartialOrd for bool
Source§fn __chaining_less(&self, other: &bool) -> ControlFlow<bool>
fn __chaining_less(&self, other: &bool) -> ControlFlow<bool>
partial_ord_chaining_methods)self == other, returns Continue(()).
Otherwise, returns Break(self < other), or equivalently Break(self <= other).
The only difference when chaining < and <= is how an equality result is
interpreted at the end of the chain. Read moreSource§fn __chaining_greater(&self, other: &bool) -> ControlFlow<bool>
fn __chaining_greater(&self, other: &bool) -> ControlFlow<bool>
partial_ord_chaining_methods)__chaining_less, but for >/>= instead of </<=.impl StructuralPartialEq for bool
impl TrivialClone for bool
1.95.0 (const: unstable) · Source§impl TryFrom<i8> for bool
impl TryFrom<i8> for bool
1.95.0 (const: unstable) · Source§impl TryFrom<i16> for bool
impl TryFrom<i16> for bool
1.95.0 (const: unstable) · Source§impl TryFrom<i32> for bool
impl TryFrom<i32> for bool
1.95.0 (const: unstable) · Source§impl TryFrom<i64> for bool
impl TryFrom<i64> for bool
1.95.0 (const: unstable) · Source§impl TryFrom<i128> for bool
impl TryFrom<i128> for bool
1.95.0 (const: unstable) · Source§impl TryFrom<u8> for bool
impl TryFrom<u8> for bool
1.95.0 (const: unstable) · Source§impl TryFrom<u16> for bool
impl TryFrom<u16> for bool
1.95.0 (const: unstable) · Source§impl TryFrom<u32> for bool
impl TryFrom<u32> for bool
1.95.0 (const: unstable) · Source§impl TryFrom<u64> for bool
impl TryFrom<u64> for bool
1.95.0 (const: unstable) · Source§impl TryFrom<u128> for bool
impl TryFrom<u128> for bool
impl UseCloned for bool
Auto Trait Implementations§
impl Forget for bool
impl Freeze for bool
impl Move for bool
impl RefUnwindSafe for bool
impl Send for bool
impl Sync for bool
impl Unpin for bool
impl UnsafeUnpin for bool
impl UnwindSafe for bool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Printable for T
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize
#[doc(hidden)]const SIZE: usize
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize
#[doc(hidden)]const ALIGN: usize
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment
#[doc(hidden)]const ALIGNMENT: Alignment
sized_type_properties)Source§#[doc(hidden)]const IS_ZST: bool
#[doc(hidden)]const IS_ZST: bool
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout
#[doc(hidden)]const LAYOUT: Layout
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize
#[doc(hidden)]const MAX_SLICE_LEN: usize
sized_type_properties)[Self]. Read more