Primitive Type bool Copy item path 1.0.0 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.
let bool_val = true & false | false ;
assert! (!bool_val);§ Examples
A trivial example of the usage of bool:
let praise_the_borrow_checker = true ;
if praise_the_borrow_checker {
println! ("oh, yeah!" );
} else {
println! ("what?!!" );
}
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 );Returns Some(t) if the bool is true ,
or None otherwise.
Arguments passed to then_some are eagerly evaluated; if you are
passing the result of a function call, it is recommended to use
then , which is lazily evaluated.
§ Examples
assert_eq! (false .then_some(0 ), None );
assert_eq! (true .then_some(0 ), Some (0 ));
let mut a = 0 ;
let mut function_with_side_effects = || { a += 1 ; };
true .then_some(function_with_side_effects());
false .then_some(function_with_side_effects());
assert_eq! (a, 2 );Returns Some(f()) if the bool is true ,
or None otherwise.
§ Examples
assert_eq! (false .then(|| 0 ), None );
assert_eq! (true .then(|| 0 ), Some (0 ));
let mut a = 0 ;
true .then(|| { a += 1 ; });
false .then(|| { a += 1 ; });
assert_eq! (a, 1 );🔬 This is a nightly-only experimental API. (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)]
assert_eq! (false .ok_or(0 ), Err (0 ));
assert_eq! (true .ok_or(0 ), Ok (()));
#![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());
assert_eq! (a, 2 );🔬 This is a nightly-only experimental API. (bool_to_result #142748 ) Returns Ok(()) if the bool is true ,
or Err(f()) otherwise.
§ Examples
#![feature(bool_to_result)]
assert_eq! (false .ok_or_else(|| 0 ), Err (0 ));
assert_eq! (true .ok_or_else(|| 0 ), Ok (()));
#![feature(bool_to_result)]
let mut a = 0 ;
assert! (true .ok_or_else(|| { a += 1 ; }).is_ok());
assert! (false .ok_or_else(|| { a += 1 ; }).is_err());
assert_eq! (a, 1 );🔬 This is a nightly-only experimental API. (atomic_internals)
Temporary implementation detail.
The resulting type after applying the & operator.
The resulting type after applying the & operator.
The resulting type after applying the & operator.
The resulting type after applying the & operator.
The resulting type after applying the | operator.
The resulting type after applying the | operator.
The resulting type after applying the | operator.
The resulting type after applying the | operator.
The resulting type after applying the ^ operator.
The resulting type after applying the ^ operator.
The resulting type after applying the ^ operator.
The resulting type after applying the ^ operator.
Performs copy-assignment from
source.
Read more Formats the value using the given formatter.
Read more Returns the default value of false
🔬 This is a nightly-only experimental API. (core_intrinsics_fallbacks)
See
super::disjoint_bitor; we just need the trait indirection to handle
different types since calling intrinsics with generics doesn’t work.
Formats the value using the given formatter.
Read more Converts from bool to
i128
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (i128::from(false ), 0 );
assert_eq! (i128::from(true ), 1 );Converts from bool to
i16
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (i16::from(false ), 0 );
assert_eq! (i16::from(true ), 1 );Converts from bool to
i32
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (i32::from(false ), 0 );
assert_eq! (i32::from(true ), 1 );Converts from bool to
i64
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (i64::from(false ), 0 );
assert_eq! (i64::from(true ), 1 );Converts from bool to
i8
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (i8::from(false ), 0 );
assert_eq! (i8::from(true ), 1 );Converts from bool to
isize
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (isize::from(false ), 0 );
assert_eq! (isize::from(true ), 1 );Converts from bool to
u128
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (u128::from(false ), 0 );
assert_eq! (u128::from(true ), 1 );Converts from bool to
u16
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (u16::from(false ), 0 );
assert_eq! (u16::from(true ), 1 );Converts from bool to
u32
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (u32::from(false ), 0 );
assert_eq! (u32::from(true ), 1 );Converts from bool to
u64
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (u64::from(false ), 0 );
assert_eq! (u64::from(true ), 1 );Converts from bool to
u8
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (u8::from(false ), 0 );
assert_eq! (u8::from(true ), 1 );Converts from bool to
usize
, by turning false into 0 and true into 1.
§ Examples
assert_eq! (usize::from(false ), 0 );
assert_eq! (usize::from(true ), 1 );The resulting type after applying the ! operator.
The resulting type after applying the ! operator.
Compares and returns the minimum of two values.
Read more Compares and returns the maximum of two values.
Read more Restrict a value to a certain interval.
Read more Tests for self and other values to be equal, and is used by ==.
Tests for !=. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
This method returns an ordering between
self and
other values if one exists.
Read more Tests less than (for
self and
other) and is used by the
< operator.
Read more Tests less than or equal to (for
self and
other) and is used by the
<= operator.
Read more Tests greater than (for
self and
other) and is used by the
>
operator.
Read more Tests greater than or equal to (for
self and
other) and is used by
the
>= operator.
Read more Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_i128 .try_into(), Ok (false ));
assert_eq! (1_i128 .try_into(), Ok (true ));
assert! (<i128 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_i16 .try_into(), Ok (false ));
assert_eq! (1_i16 .try_into(), Ok (true ));
assert! (<i16 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_i32 .try_into(), Ok (false ));
assert_eq! (1_i32 .try_into(), Ok (true ));
assert! (<i32 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_i64 .try_into(), Ok (false ));
assert_eq! (1_i64 .try_into(), Ok (true ));
assert! (<i64 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_i8 .try_into(), Ok (false ));
assert_eq! (1_i8 .try_into(), Ok (true ));
assert! (<i8 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_u128 .try_into(), Ok (false ));
assert_eq! (1_u128 .try_into(), Ok (true ));
assert! (<u128 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_u16 .try_into(), Ok (false ));
assert_eq! (1_u16 .try_into(), Ok (true ));
assert! (<u16 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_u32 .try_into(), Ok (false ));
assert_eq! (1_u32 .try_into(), Ok (true ));
assert! (<u32 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_u64 .try_into(), Ok (false ));
assert_eq! (1_u64 .try_into(), Ok (true ));
assert! (<u64 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Tries to create a bool from an integer type.
Returns an error if the integer is not 0 or 1.
§ Examples
assert_eq! (0_u8 .try_into(), Ok (false ));
assert_eq! (1_u8 .try_into(), Ok (true ));
assert! (<u8 as TryInto<bool>>::try_into(2 ).is_err());The type returned in the event of a conversion error.
Immutably borrows from an owned value.
Read more Mutably borrows from an owned value.
Read more Returns the argument unchanged.
Calls U::from(self).
That is, this conversion is whatever the implementation of
From <T> for U chooses to do.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.