Primitive Type bool1.0.0
[−]
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.
bool
implements various traits, such as BitAnd
, BitOr
, Not
, etc.,
which allow us to perform boolean operations using &
, |
and !
.
if
always demands a bool
value. assert!
, being an important macro in testing,
checks whether an expression returns true
.
let bool_val = true & false | false;
assert!(!bool_val);Run
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!"),
}Run
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);Run
The resulting type after applying the &
operator.
Performs the &
operation.
The resulting type after applying the &
operator.
Performs the &
operation.
The resulting type after applying the &
operator.
Performs the &
operation.
The resulting type after applying the &
operator.
Performs the &
operation.
Performs the |=
operation.
Performs the |=
operation.
Formats the value using the given formatter. Read more
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self | 1.21.0 [src] |
[−]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self | 1.21.0 [src] |
[−]
Compares and returns the minimum of two values. Read more
The resulting type after applying the ^
operator.
Performs the ^
operation.
The resulting type after applying the ^
operator.
Performs the ^
operation.
The resulting type after applying the ^
operator.
Performs the ^
operation.
The resulting type after applying the ^
operator.
Performs the ^
operation.
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
The resulting type after applying the !
operator.
Performs the unary !
operation.
The resulting type after applying the !
operator.
Performs the unary !
operation.
The associated error which can be returned from parsing.
Parse a bool
from a string.
Yields a Result<bool, ParseBoolError>
, because s
may or may not
actually be parseable.
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());Run
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());Run
Formats the value using the given formatter. Read more
Performs the ^=
operation.
Performs the ^=
operation.
Performs the &=
operation.
Performs the &=
operation.
The resulting type after applying the |
operator.
Performs the |
operation.
The resulting type after applying the |
operator.
Performs the |
operation.
The resulting type after applying the |
operator.
Performs the |
operation.
The resulting type after applying the |
operator.
Performs the |
operation.
Returns the default value of false
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