|
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 | +## Operations on boolean values |
| 36 | + |
| 37 | +<!-- This is washy wording --> When using certain operator expressions with a |
| 38 | +boolean type for its operands, they evaluate using the rules of [boolean logic]. |
| 39 | + |
| 40 | +### Logical not |
| 41 | + |
| 42 | +| `b` | [`!b`][op-not] | |
| 43 | +|- | - | |
| 44 | +| `true` | `false` | |
| 45 | +| `false` | `true` | |
| 46 | + |
| 47 | +### Logical or |
| 48 | + |
| 49 | +| `a` | `b` | [<code>a | b</code>][op-or] | |
| 50 | +|- | - | - | |
| 51 | +| `true` | `true` | `true` | |
| 52 | +| `true` | `false` | `true` | |
| 53 | +| `false` | `true` | `true` | |
| 54 | +| `false` | `false` | `false` | |
| 55 | + |
| 56 | +### Logical and |
| 57 | + |
| 58 | +| `a` | `b` | [`a & b`][op-and] | |
| 59 | +|- | - | - | |
| 60 | +| `true` | `true` | `true` | |
| 61 | +| `true` | `false` | `false` | |
| 62 | +| `false` | `true` | `false` | |
| 63 | +| `false` | `false` | `false` | |
| 64 | + |
| 65 | +### Logical xor |
| 66 | + |
| 67 | +| `a` | `b` | [`a ^ b`][op-xor] | |
| 68 | +|- | - | - | |
| 69 | +| `true` | `true` | `false` | |
| 70 | +| `true` | `false` | `true` | |
| 71 | +| `false` | `true` | `true` | |
| 72 | +| `false` | `false` | `false` | |
| 73 | + |
| 74 | +### Comparisons |
| 75 | + |
| 76 | +| `a` | `b` | [`a == b`][op-compare] | |
| 77 | +|- | - | - | |
| 78 | +| `true` | `true` | `true` | |
| 79 | +| `true` | `false` | `false` | |
| 80 | +| `false` | `true` | `false` | |
| 81 | +| `false` | `false` | `true` | |
| 82 | + |
| 83 | +| `a` | `b` | [`a > b`][op-compare] | |
| 84 | +|- | - | - | |
| 85 | +| `true` | `true` | `false` | |
| 86 | +| `true` | `false` | `true` | |
| 87 | +| `false` | `true` | `false` | |
| 88 | +| `false` | `false` | `false` | |
| 89 | + |
| 90 | +* `a != b` is the same as `!(a == b)` |
| 91 | +* `a >= b` is the same as `a == b | a > b` |
| 92 | +* `a < b` is the same as `!(a >= b)` |
| 93 | +* `a <= b` is the same as `a == b | a < b` |
| 94 | + |
| 95 | +[boolean logic]: https://en.wikipedia.org/wiki/Boolean_algebra |
| 96 | +[enumerated type]: enum.md |
| 97 | +[expressions]: ../expressions.md |
| 98 | +[if expressions]: ../expressions/if-expr.md#if-expressions |
| 99 | +[language prelude]: ../names/preludes.md#language-prelude |
| 100 | +[lazy]: ../expressions/operator-expr.md#lazy-boolean-operators |
| 101 | +[literal expression]: ../expressions/literal-expr.md |
| 102 | +[name]: ../names.md |
| 103 | +[op-and]: ../expressions/operator-expr.md#arithmetic-and-logical-binary-operators |
| 104 | +[op-compare]: ../expressions/operator-expr.md#comparison-operators |
| 105 | +[op-not]: ../expressions/operator-expr.md#negation-operators |
| 106 | +[op-or]: ../expressions/operator-expr.md#arithmetic-and-logical-binary-operators |
| 107 | +[op-xor]: ../expressions/operator-expr.md#arithmetic-and-logical-binary-operators |
| 108 | +[p-clone]: ../special-types-and-traits.md#clone |
| 109 | +[p-copy]: ../special-types-and-traits.md#copy |
| 110 | +[p-impl]: ../items/implementations.md |
| 111 | +[p-send]: ../special-types-and-traits.md#send |
| 112 | +[p-sized]: ../special-types-and-traits.md#sized |
| 113 | +[p-sync]: ../special-types-and-traits.md#sync |
| 114 | +[p-traits]: ../items/traits.md |
| 115 | +[size and alignment]: ../type-layout.md#size-and-alignment |
| 116 | +[std]: ../../std/primitive.bool.html |
| 117 | +[undefined behavior]: ../behavior-considered-undefined.md |
| 118 | +[while expressions]: ../expressions/loop-expr.md#predicate-loops |
0 commit comments