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);Source§
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 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());
// `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);Source 🔬This is a nightly-only experimental API. (bool_to_result #142748)
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());
// `a` is incremented twice because the value passed to `ok_or` is
// evaluated eagerly.
assert_eq!(a, 2);Source 🔬This is a nightly-only experimental API. (bool_to_result #142748)
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());
// `a` is incremented once because the closure is evaluated lazily by
// `ok_or_else`.
assert_eq!(a, 1);Source§
Source§
🔬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.
1.0.0 · Source§
Source§
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());