bool - Rust (original) (raw)

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 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);

Source§

Source§

🔬This is a nightly-only experimental API. (atomic_internals)

Temporary implementation detail.

1.0.0 · Source§

Source§

The resulting type after applying the & operator.

Source§

1.0.0 · Source§

Source§

The resulting type after applying the & operator.

Source§

Source§

Source§

The resulting type after applying the & operator.

Source§

1.0.0 · Source§

Source§

The resulting type after applying the & operator.

Source§

Source§

Source§

The resulting type after applying the & operator.

Source§

1.0.0 · Source§

Source§

The resulting type after applying the & operator.

Source§

1.22.0 · Source§

Source§

1.8.0 · Source§

1.0.0 · Source§

Source§

The resulting type after applying the | operator.

Source§

1.0.0 · Source§

Source§

The resulting type after applying the | operator.

Source§

Source§

Source§

The resulting type after applying the | operator.

Source§

1.0.0 · Source§

Source§

The resulting type after applying the | operator.

Source§

Source§

Source§

The resulting type after applying the | operator.

Source§

1.0.0 · Source§

Source§

The resulting type after applying the | operator.

Source§

1.22.0 · Source§

Source§

1.8.0 · Source§

1.0.0 · Source§

Source§

The resulting type after applying the ^ operator.

Source§

1.0.0 · Source§

Source§

The resulting type after applying the ^ operator.

Source§

Source§

Source§

The resulting type after applying the ^ operator.

Source§

1.0.0 · Source§

Source§

The resulting type after applying the ^ operator.

Source§

Source§

Source§

The resulting type after applying the ^ operator.

Source§

1.0.0 · Source§

Source§

The resulting type after applying the ^ operator.

Source§

1.22.0 · Source§

Source§

1.8.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

Source§

Returns the default value of false

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§

1.24.0 · Source§

Source§

Converts a bool into an AtomicBool.

§Examples
use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{atomic_bool:?}"), "true")

1.68.0 · Source§

Source§

Converts a bool to f128 losslessly. The resulting value is positive 0.0 for false and 1.0 for true values.

§Examples
#![feature(f128)]

let x: f128 = false.into();
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());

let y: f128 = true.into();
assert_eq!(y, 1.0);

1.68.0 · Source§

Source§

Converts a bool to f16 losslessly. The resulting value is positive 0.0 for false and 1.0 for true values.

§Examples
#![feature(f16)]

let x: f16 = false.into();
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());

let y: f16 = true.into();
assert_eq!(y, 1.0);

1.68.0 · Source§

Source§

Converts a bool to f32 losslessly. The resulting value is positive 0.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);

1.68.0 · Source§

Source§

Converts a bool to f64 losslessly. The resulting value is positive 0.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);

1.28.0 · Source§

Source§

Converts a bool to i128 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to i16 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to i32 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to i64 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to i8 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to isize losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to u128 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to u16 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to u32 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to u64 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to u8 losslessly. 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);

1.28.0 · Source§

Source§

Converts a bool to usize losslessly. 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);

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());

Source§

The associated error which can be returned from parsing.

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

Source§

Tests for self and other values to be equal, and is used by ==.

Source§

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

1.0.0 · Source§

Source§

This method returns an ordering between self and other values if one exists. Read more

Source§

Tests less than (for self and other) and is used by the < operator. Read more

Source§

Tests less than or equal to (for self and other) and is used by the<= operator. Read more

Source§

Tests greater than (for self and other) and is used by the >operator. Read more

Source§

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Source§

Source§

🔬This is a nightly-only experimental API. (random #130703)

Generates a random value.

Source§

1.0.0 · Source§

1.0.0 · Source§

Source§

Source§

Source§

§

§

§

§

§

§