Skip to main content

bool

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

Implementationsยง

Sourceยง

impl bool

1.62.0 (const: unstable) ยท Source

pub fn then_some<T>(self, t: T) -> Option<T>

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 (const: unstable) ยท Source

pub fn then<T, F>(self, f: F) -> Option<T>
where F: FnOnce() -> T,

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);
1.98.0 (const: unstable) ยท Source

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
assert_eq!(false.ok_or(0), Err(0));
assert_eq!(true.ok_or(0), Ok(()));
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);
1.98.0 (const: unstable) ยท Source

pub fn ok_or_else<E, F>(self, f: F) -> Result<(), E>
where F: FnOnce() -> E,

Returns Ok(()) if the bool is true, or Err(f()) otherwise.

ยงExamples
assert_eq!(false.ok_or_else(|| 0), Err(0));
assert_eq!(true.ok_or_else(|| 0), Ok(()));
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);
1.100.0 (const: 1.100.0) ยท Source

pub const fn toggle(&mut self)

Toggles self in-place.

Equivalent to value = !value.

ยงExamples
let mut boolean = false;

boolean.toggle();
assert_eq!(boolean, true);

boolean.toggle();
assert_eq!(boolean, false);

Trait Implementationsยง

Sourceยง

impl AtomicPrimitive for bool

Available on target_has_atomic_load_store=8 only.
Sourceยง

type Storage = Align1<u8>

๐Ÿ”ฌThis is a nightly-only experimental API. (atomic_internals)
Temporary implementation detail.
1.0.0 (const: unstable) ยท Sourceยง

impl BitAnd for bool

Sourceยง

type Output = bool

The resulting type after applying the & operator.
Sourceยง

fn bitand(self, rhs: bool) -> bool

Performs the & operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitAnd<&bool> for bool

Sourceยง

type Output = <bool as BitAnd>::Output

The resulting type after applying the & operator.
Sourceยง

fn bitand(self, other: &bool) -> <bool as BitAnd>::Output

Performs the & operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitAnd<&bool> for &bool

Sourceยง

type Output = <bool as BitAnd>::Output

The resulting type after applying the & operator.
Sourceยง

fn bitand(self, other: &bool) -> <bool as BitAnd>::Output

Performs the & operation. Read more
Sourceยง

impl<T, const N: usize> BitAnd<Mask<T, N>> for bool
where T: MaskElement,

Sourceยง

type Output = Mask<T, N>

The resulting type after applying the & operator.
Sourceยง

fn bitand(self, rhs: Mask<T, N>) -> Mask<T, N>

Performs the & operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitAnd<bool> for &bool

Sourceยง

type Output = <bool as BitAnd>::Output

The resulting type after applying the & operator.
Sourceยง

fn bitand(self, other: bool) -> <bool as BitAnd>::Output

Performs the & operation. Read more
Sourceยง

impl<T, const N: usize> BitAnd<bool> for Mask<T, N>
where T: MaskElement,

Sourceยง

type Output = Mask<T, N>

The resulting type after applying the & operator.
Sourceยง

fn bitand(self, rhs: bool) -> Mask<T, N>

Performs the & operation. Read more
1.8.0 (const: unstable) ยท Sourceยง

impl BitAndAssign for bool

Sourceยง

fn bitand_assign(&mut self, other: bool)

Performs the &= operation. Read more
1.22.0 (const: unstable) ยท Sourceยง

impl BitAndAssign<&bool> for bool

Sourceยง

fn bitand_assign(&mut self, other: &bool)

Performs the &= operation. Read more
Sourceยง

impl<T, const N: usize> BitAndAssign<bool> for Mask<T, N>
where T: MaskElement,

Sourceยง

fn bitand_assign(&mut self, rhs: bool)

Performs the &= operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitOr for bool

Sourceยง

type Output = bool

The resulting type after applying the | operator.
Sourceยง

fn bitor(self, rhs: bool) -> bool

Performs the | operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitOr<&bool> for bool

Sourceยง

type Output = <bool as BitOr>::Output

The resulting type after applying the | operator.
Sourceยง

fn bitor(self, other: &bool) -> <bool as BitOr>::Output

Performs the | operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitOr<&bool> for &bool

Sourceยง

type Output = <bool as BitOr>::Output

The resulting type after applying the | operator.
Sourceยง

fn bitor(self, other: &bool) -> <bool as BitOr>::Output

Performs the | operation. Read more
Sourceยง

impl<T, const N: usize> BitOr<Mask<T, N>> for bool
where T: MaskElement,

Sourceยง

type Output = Mask<T, N>

The resulting type after applying the | operator.
Sourceยง

fn bitor(self, rhs: Mask<T, N>) -> Mask<T, N>

Performs the | operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitOr<bool> for &bool

Sourceยง

type Output = <bool as BitOr>::Output

The resulting type after applying the | operator.
Sourceยง

fn bitor(self, other: bool) -> <bool as BitOr>::Output

Performs the | operation. Read more
Sourceยง

impl<T, const N: usize> BitOr<bool> for Mask<T, N>
where T: MaskElement,

Sourceยง

type Output = Mask<T, N>

The resulting type after applying the | operator.
Sourceยง

fn bitor(self, rhs: bool) -> Mask<T, N>

Performs the | operation. Read more
1.8.0 (const: unstable) ยท Sourceยง

impl BitOrAssign for bool

Sourceยง

fn bitor_assign(&mut self, other: bool)

Performs the |= operation. Read more
1.22.0 (const: unstable) ยท Sourceยง

impl BitOrAssign<&bool> for bool

Sourceยง

fn bitor_assign(&mut self, other: &bool)

Performs the |= operation. Read more
Sourceยง

impl<T, const N: usize> BitOrAssign<bool> for Mask<T, N>
where T: MaskElement,

Sourceยง

fn bitor_assign(&mut self, rhs: bool)

Performs the |= operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitXor for bool

Sourceยง

type Output = bool

The resulting type after applying the ^ operator.
Sourceยง

fn bitxor(self, other: bool) -> bool

Performs the ^ operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitXor<&bool> for bool

Sourceยง

type Output = <bool as BitXor>::Output

The resulting type after applying the ^ operator.
Sourceยง

fn bitxor(self, other: &bool) -> <bool as BitXor>::Output

Performs the ^ operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitXor<&bool> for &bool

Sourceยง

type Output = <bool as BitXor>::Output

The resulting type after applying the ^ operator.
Sourceยง

fn bitxor(self, other: &bool) -> <bool as BitXor>::Output

Performs the ^ operation. Read more
Sourceยง

impl<T, const N: usize> BitXor<Mask<T, N>> for bool
where T: MaskElement,

Sourceยง

type Output = Mask<T, N>

The resulting type after applying the ^ operator.
Sourceยง

fn bitxor(self, rhs: Mask<T, N>) -> <bool as BitXor<Mask<T, N>>>::Output

Performs the ^ operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl BitXor<bool> for &bool

Sourceยง

type Output = <bool as BitXor>::Output

The resulting type after applying the ^ operator.
Sourceยง

fn bitxor(self, other: bool) -> <bool as BitXor>::Output

Performs the ^ operation. Read more
Sourceยง

impl<T, const N: usize> BitXor<bool> for Mask<T, N>
where T: MaskElement,

Sourceยง

type Output = Mask<T, N>

The resulting type after applying the ^ operator.
Sourceยง

fn bitxor(self, rhs: bool) -> <Mask<T, N> as BitXor<bool>>::Output

Performs the ^ operation. Read more
1.8.0 (const: unstable) ยท Sourceยง

impl BitXorAssign for bool

Sourceยง

fn bitxor_assign(&mut self, other: bool)

Performs the ^= operation. Read more
1.22.0 (const: unstable) ยท Sourceยง

impl BitXorAssign<&bool> for bool

Sourceยง

fn bitxor_assign(&mut self, other: &bool)

Performs the ^= operation. Read more
Sourceยง

impl<T, const N: usize> BitXorAssign<bool> for Mask<T, N>
where T: MaskElement,

Sourceยง

fn bitxor_assign(&mut self, rhs: bool)

Performs the ^= operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl Clone for bool

Sourceยง

fn clone(&self) -> bool

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl ConstParamTy_ for bool

1.0.0 ยท Sourceยง

impl Copy for bool

1.0.0 ยท Sourceยง

impl Debug for bool

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl Default for bool

Sourceยง

fn default() -> bool

Returns the default value of false

Sourceยง

impl DisjointBitOr for bool

Sourceยง

unsafe fn disjoint_bitor(self, other: bool) -> bool

๐Ÿ”ฌ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ยง

impl Display for bool

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Sourceยง

impl Distribution<bool> for RangeFull

Sourceยง

fn sample(&self, source: &mut (impl Rng + ?Sized)) -> bool

๐Ÿ”ฌThis is a nightly-only experimental API. (random #130703)
Samples a random value from the distribution, using the specified random source.
1.0.0 (const: unstable) ยท Sourceยง

impl Eq for bool

1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for u8

Sourceยง

fn from(b: bool) -> u8

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for u16

Sourceยง

fn from(b: bool) -> u16

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for u32

Sourceยง

fn from(b: bool) -> u32

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for u64

Sourceยง

fn from(b: bool) -> u64

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for u128

Sourceยง

fn from(b: bool) -> u128

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for usize

Sourceยง

fn from(b: bool) -> usize

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for i8

Sourceยง

fn from(b: bool) -> i8

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for i16

Sourceยง

fn from(b: bool) -> i16

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for i32

Sourceยง

fn from(b: bool) -> i32

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for i64

Sourceยง

fn from(b: bool) -> i64

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for i128

Sourceยง

fn from(b: bool) -> i128

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);
1.28.0 (const: unstable) ยท Sourceยง

impl From<bool> for isize

Sourceยง

fn from(b: bool) -> isize

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);
Sourceยง

impl From<bool> for f16

Sourceยง

fn from(small: bool) -> f16

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::from(false);
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());

let y = f16::from(true);
assert_eq!(y, 1.0);
1.68.0 (const: unstable) ยท Sourceยง

impl From<bool> for f32

Sourceยง

fn from(small: bool) -> f32

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::from(false);
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());

let y = f32::from(true);
assert_eq!(y, 1.0);
1.68.0 (const: unstable) ยท Sourceยง

impl From<bool> for f64

Sourceยง

fn from(small: bool) -> f64

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::from(false);
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());

let y = f64::from(true);
assert_eq!(y, 1.0);
Sourceยง

impl From<bool> for f128

Sourceยง

fn from(small: bool) -> f128

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::from(false);
assert_eq!(x, 0.0);
assert!(x.is_sign_positive());

let y = f128::from(true);
assert_eq!(y, 1.0);
1.24.0 (const: unstable) ยท Sourceยง

impl From<bool> for Atomic<bool>

Available on target_has_atomic_load_store=8 only.
Sourceยง

fn from(b: bool) -> Atomic<bool>

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.0.0 ยท Sourceยง

impl FromStr for bool

Sourceยง

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.

assert_eq!("true".parse(), Ok(true));
assert_eq!("false".parse(), Ok(false));
assert!("not even a boolean".parse::<bool>().is_err());
Sourceยง

type Err = ParseBoolError

The associated error which can be returned from parsing.
1.0.0 ยท Sourceยง

impl Hash for bool

Sourceยง

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 ยท Sourceยง

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl Not for bool

Sourceยง

type Output = bool

The resulting type after applying the ! operator.
Sourceยง

fn not(self) -> bool

Performs the unary ! operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl Not for &bool

Sourceยง

type Output = <bool as Not>::Output

The resulting type after applying the ! operator.
Sourceยง

fn not(self) -> <bool as Not>::Output

Performs the unary ! operation. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl Ord for bool

Sourceยง

fn cmp(&self, other: &bool) -> Ordering

This method returns an Ordering between self and other. Read more
Sourceยง

fn min(self, other: bool) -> bool

Compares and returns the minimum of two values. Read more
Sourceยง

fn max(self, other: bool) -> bool

Compares and returns the maximum of two values. Read more
Sourceยง

fn clamp(self, min: bool, max: bool) -> bool

Restrict a value to a certain interval. Read more
Sourceยง

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

๐Ÿ”ฌThis is a nightly-only experimental API. (clamp_to #147781)
Restrict a value to a certain range. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl PartialEq for bool

Sourceยง

fn eq(&self, other: &bool) -> bool

Equality operator ==. Read more
Sourceยง

fn ne(&self, other: &bool) -> bool

Inequality operator !=. Read more
1.0.0 (const: unstable) ยท Sourceยง

impl PartialOrd for bool

Sourceยง

fn partial_cmp(&self, other: &bool) -> Option<Ordering>

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

fn lt(&self, other: &bool) -> bool

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

fn le(&self, other: &bool) -> bool

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

fn gt(&self, other: &bool) -> bool

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

fn ge(&self, other: &bool) -> bool

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

impl StructuralPartialEq for bool

1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<i8> for bool

Sourceยง

fn try_from(i: i8) -> Result<bool, <bool as TryFrom<i8>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_i8), Ok(false));

assert_eq!(bool::try_from(1_i8), Ok(true));

assert!(bool::try_from(2_i8).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<i16> for bool

Sourceยง

fn try_from(i: i16) -> Result<bool, <bool as TryFrom<i16>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_i16), Ok(false));

assert_eq!(bool::try_from(1_i16), Ok(true));

assert!(bool::try_from(2_i16).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<i32> for bool

Sourceยง

fn try_from(i: i32) -> Result<bool, <bool as TryFrom<i32>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_i32), Ok(false));

assert_eq!(bool::try_from(1_i32), Ok(true));

assert!(bool::try_from(2_i32).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<i64> for bool

Sourceยง

fn try_from(i: i64) -> Result<bool, <bool as TryFrom<i64>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_i64), Ok(false));

assert_eq!(bool::try_from(1_i64), Ok(true));

assert!(bool::try_from(2_i64).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<i128> for bool

Sourceยง

fn try_from(i: i128) -> Result<bool, <bool as TryFrom<i128>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_i128), Ok(false));

assert_eq!(bool::try_from(1_i128), Ok(true));

assert!(bool::try_from(2_i128).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<u8> for bool

Sourceยง

fn try_from(i: u8) -> Result<bool, <bool as TryFrom<u8>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_u8), Ok(false));

assert_eq!(bool::try_from(1_u8), Ok(true));

assert!(bool::try_from(2_u8).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<u16> for bool

Sourceยง

fn try_from(i: u16) -> Result<bool, <bool as TryFrom<u16>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_u16), Ok(false));

assert_eq!(bool::try_from(1_u16), Ok(true));

assert!(bool::try_from(2_u16).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<u32> for bool

Sourceยง

fn try_from(i: u32) -> Result<bool, <bool as TryFrom<u32>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_u32), Ok(false));

assert_eq!(bool::try_from(1_u32), Ok(true));

assert!(bool::try_from(2_u32).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<u64> for bool

Sourceยง

fn try_from(i: u64) -> Result<bool, <bool as TryFrom<u64>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_u64), Ok(false));

assert_eq!(bool::try_from(1_u64), Ok(true));

assert!(bool::try_from(2_u64).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
1.95.0 (const: unstable) ยท Sourceยง

impl TryFrom<u128> for bool

Sourceยง

fn try_from(i: u128) -> Result<bool, <bool as TryFrom<u128>>::Error>

Tries to create a bool from an integer type. Returns an error if the integer is not 0 or 1.

ยงExamples
assert_eq!(bool::try_from(0_u128), Ok(false));

assert_eq!(bool::try_from(1_u128), Ok(true));

assert!(bool::try_from(2_u128).is_err());
Sourceยง

type Error = TryFromIntError

The type returned in the event of a conversion error.
Sourceยง

impl UseCloned for bool

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Sourceยง

impl<T> ToString for T
where T: Display + ?Sized,

Sourceยง

fn to_string(&self) -> String

Converts the given value to a String. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = !

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.