let b: bool = true;
The boolean type or bool is a primitive data type that can take on one of two values, called true and false.
Values of this type may be created using a literal expression using the
keywords true
and false
corresponding to the value of the same name.
This type is a part of the language prelude with the name bool
.
An object with the boolean type has a size and alignment of 1 each. The
value false has the bit pattern 0x00
and the value true has the bit pattern
0x01
. It is undefined behavior for an object with the boolean type to have
any other bit pattern.
The boolean type is the type of many operands in various expressions:
- The condition operand in if expressions and while expressions
- The operands in lazy boolean operator expressions
Note: The boolean type acts similarly to but is not an enumerated type. In practice, this mostly means that constructors are not associated to the type (e.g.
bool::true
).
Like all primitives, the boolean type implements the
traits Clone
, Copy
, Sized
,
Send
, and Sync
.
When using certain operator expressions with aNote: See the standard library docs for library operations.
boolean type for its operands, they evaluate using the rules of boolean logic.
b |
!b |
---|---|
true |
false |
false |
true |
a |
b |
a | b |
---|---|---|
true |
true |
true |
true |
false |
true |
false |
true |
true |
false |
false |
false |
a |
b |
a & b |
---|---|---|
true |
true |
true |
true |
false |
false |
false |
true |
false |
false |
false |
false |
a |
b |
a ^ b |
---|---|---|
true |
true |
false |
true |
false |
true |
false |
true |
true |
false |
false |
false |
a |
b |
a == b |
---|---|---|
true |
true |
true |
true |
false |
false |
false |
true |
false |
false |
false |
true |
a |
b |
a > b |
---|---|---|
true |
true |
false |
true |
false |
true |
false |
true |
false |
false |
false |
false |
a != b
is the same as!(a == b)
a >= b
is the same asa == b | a > b
a < b
is the same as!(a >= b)
a <= b
is the same asa == b | a < b