bool - Rust (original) (raw)
Primitive Type bool
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;
// 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);
1.62.0 · source
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 usethen, 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());
// `a` is incremented twice because the value passed to `then_some` is
// evaluated eagerly.
assert_eq!(a, 2);
1.50.0 · source
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; });
// `a` is incremented once because the closure is evaluated lazily by
// `then`.
assert_eq!(a, 1);
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.
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.
Returns the default value of false
Converts a bool
into an AtomicBool
.
Examples
use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{atomic_bool:?}"), "true")
Converts bool
to f32
losslessly. The resulting value is positive0.0
for false
and 1.0
for true
values.
Examples
let x: f32 = false.into();
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());
let y: f32 = true.into();
assert_eq!(y, 1.0);
Converts bool
to f64
losslessly. The resulting value is positive0.0
for false
and 1.0
for true
values.
Examples
let x: f64 = false.into();
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());
let y: f64 = true.into();
assert_eq!(y, 1.0);
Converts a bool
to a i128
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i128::from(true), 1);
assert_eq!(i128::from(false), 0);
Converts a bool
to a i16
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i16::from(true), 1);
assert_eq!(i16::from(false), 0);
Converts a bool
to a i32
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i32::from(true), 1);
assert_eq!(i32::from(false), 0);
Converts a bool
to a i64
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i64::from(true), 1);
assert_eq!(i64::from(false), 0);
Converts a bool
to a i8
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(i8::from(true), 1);
assert_eq!(i8::from(false), 0);
Converts a bool
to a isize
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(isize::from(true), 1);
assert_eq!(isize::from(false), 0);
Converts a bool
to a u128
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u128::from(true), 1);
assert_eq!(u128::from(false), 0);
Converts a bool
to a u16
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u16::from(true), 1);
assert_eq!(u16::from(false), 0);
Converts a bool
to a u32
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u32::from(true), 1);
assert_eq!(u32::from(false), 0);
Converts a bool
to a u64
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u64::from(true), 1);
assert_eq!(u64::from(false), 0);
Converts a bool
to a u8
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(u8::from(true), 1);
assert_eq!(u8::from(false), 0);
Converts a bool
to a usize
. The resulting value is 0
for false
and 1
for true
values.
Examples
assert_eq!(usize::from(true), 1);
assert_eq!(usize::from(false), 0);
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.
assert_eq!("true".parse(), Ok(true));
assert_eq!("false".parse(), Ok(false));
assert!("not even a boolean".parse::<bool>().is_err());
The associated error which can be returned from parsing.
The resulting type after applying the !
operator.
Performs the unary !
operation. Read more
The resulting type after applying the !
operator.
Performs the unary !
operation. Read more
This method tests for self
and other
values to be equal, and is used by ==
.
This method 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
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more