|
1 | 1 | # Boolean type
|
2 | 2 |
|
3 |
| -The `bool` type is a datatype which can be either `true` or `false`. The boolean |
4 |
| -type uses one byte of memory. It is used in comparisons and bitwise operations |
5 |
| -like `&`, `|`, and `!`. |
6 |
| - |
7 | 3 | ```rust
|
8 |
| -fn main() { |
9 |
| - let x = true; |
10 |
| - let y: bool = false; // with the boolean type annotation |
11 |
| - |
12 |
| - // Use of booleans in conditional expressions |
13 |
| - if x { |
14 |
| - println!("x is true"); |
15 |
| - } |
16 |
| -} |
| 4 | +let b: bool = true; |
17 | 5 | ```
|
| 6 | + |
| 7 | +The *boolean type* or *bool* is a primitive data type that can take on one of |
| 8 | +two values, called *true* and *false*. |
| 9 | + |
| 10 | +Values of this type may be created using a [literal expression] using the |
| 11 | +keywords `true` and `false` corresponding to the value of the same name. |
| 12 | + |
| 13 | +This type is a part of the [language prelude] with the [name] `bool`. |
| 14 | + |
| 15 | +An object with the boolean type has a [size and alignment] of 1 each. The |
| 16 | +value false has the bit pattern `0x00` and the value true has the bit pattern |
| 17 | +`0x01`. It is [undefined behavior] for an object with the boolean type to have |
| 18 | +any other bit pattern. |
| 19 | + |
| 20 | +The boolean type is the type of many operands in various [expressions]: |
| 21 | + |
| 22 | +* The condition operand in [if expressions] and [while expressions] |
| 23 | +* The operands in [lazy boolean operator expressions][lazy] |
| 24 | + |
| 25 | +> **Note**: The boolean type acts similarly to but is not an [enumerated type]. |
| 26 | +In practice, this mostly means that constructors are not associated to the type |
| 27 | +(e.g. `bool::true`). |
| 28 | + |
| 29 | +Like all primitives, the boolean type [implements][p-impl] the |
| 30 | +[traits][p-traits] [`Clone`][p-clone], [`Copy`][p-copy], [`Sized`][p-sized], |
| 31 | +[`Send`][p-send], and [`Sync`][p-sync]. |
| 32 | + |
| 33 | +> **Note**: See the [standard library docs][std] for library operations. |
| 34 | +
|
| 35 | +[enumerated type]: enum.md |
| 36 | +[expressions]: ../expressions.md |
| 37 | +[if expressions]: ../expressions/if-expr.md#if-expressions |
| 38 | +[language prelude]: ../names/preludes.md#language-prelude |
| 39 | +[lazy]: ../expressions/operator-expr.md#lazy-boolean-operators |
| 40 | +[literal expression]: ../expressions/literal-expr.md |
| 41 | +[name]: ../names.md |
| 42 | +[p-clone]: ../special-types-and-traits.md#clone |
| 43 | +[p-copy]: ../special-types-and-traits.md#copy |
| 44 | +[p-impl]: ../items/implementations.md |
| 45 | +[p-send]: ../special-types-and-traits.md#send |
| 46 | +[p-sized]: ../special-types-and-traits.md#sized |
| 47 | +[p-sync]: ../special-types-and-traits.md#sync |
| 48 | +[p-traits]: ../items/traits.md |
| 49 | +[size and alignment]: ../type-layout.md#size-and-alignment |
| 50 | +[std]: ../../std/primitive.bool.html |
| 51 | +[undefined behavior]: ../behavior-considered-undefined.md |
| 52 | +[while expressions]: ../expressions/loop-expr.md#predicate-loops |
0 commit comments