Skip to content

Commit 1804726

Browse files
committed
Document inherent bool op expr evaluation
1 parent 7c6e0c0 commit 1804726

File tree

2 files changed

+86
-16
lines changed

2 files changed

+86
-16
lines changed

src/expressions/operator-expr.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ for other types. Remember that signed integers are always represented using
164164
two's complement. The operands of all of these operators are evaluated in
165165
[value expression context][value expression] so are moved or copied.
166166

167-
| Symbol | Integer | `bool` | Floating Point | Overloading Trait |
168-
|--------|-------------|-------------|----------------|--------------------|
169-
| `-` | Negation* | | Negation | `std::ops::Neg` |
170-
| `!` | Bitwise NOT | Logical NOT | | `std::ops::Not` |
167+
| Symbol | Integer | `bool` | Floating Point | Overloading Trait |
168+
|--------|-------------|-------------- |----------------|--------------------|
169+
| `-` | Negation* | | Negation | `std::ops::Neg` |
170+
| `!` | Bitwise NOT | [Logical NOT] | | `std::ops::Not` |
171171

172172
\* Only for signed integer types.
173173

@@ -202,18 +202,18 @@ types. Remember that signed integers are always represented using two's
202202
complement. The operands of all of these operators are evaluated in [value
203203
expression context][value expression] so are moved or copied.
204204

205-
| Symbol | Integer | `bool` | Floating Point | Overloading Trait | Overloading Compound Assignment Trait |
206-
|--------|-------------------------|-------------|----------------|--------------------| ------------------------------------- |
207-
| `+` | Addition | | Addition | `std::ops::Add` | `std::ops::AddAssign` |
208-
| `-` | Subtraction | | Subtraction | `std::ops::Sub` | `std::ops::SubAssign` |
209-
| `*` | Multiplication | | Multiplication | `std::ops::Mul` | `std::ops::MulAssign` |
210-
| `/` | Division* | | Division | `std::ops::Div` | `std::ops::DivAssign` |
211-
| `%` | Remainder | | Remainder | `std::ops::Rem` | `std::ops::RemAssign` |
212-
| `&` | Bitwise AND | Logical AND | | `std::ops::BitAnd` | `std::ops::BitAndAssign` |
213-
| <code>&#124;</code> | Bitwise OR | Logical OR | | `std::ops::BitOr` | `std::ops::BitOrAssign` |
214-
| `^` | Bitwise XOR | Logical XOR | | `std::ops::BitXor` | `std::ops::BitXorAssign` |
215-
| `<<` | Left Shift | | | `std::ops::Shl` | `std::ops::ShlAssign` |
216-
| `>>` | Right Shift** | | | `std::ops::Shr` | `std::ops::ShrAssign` |
205+
| Symbol | Integer | `bool` | Floating Point | Overloading Trait | Overloading Compound Assignment Trait |
206+
|--------|-------------------------|---------------|----------------|--------------------| ------------------------------------- |
207+
| `+` | Addition | | Addition | `std::ops::Add` | `std::ops::AddAssign` |
208+
| `-` | Subtraction | | Subtraction | `std::ops::Sub` | `std::ops::SubAssign` |
209+
| `*` | Multiplication | | Multiplication | `std::ops::Mul` | `std::ops::MulAssign` |
210+
| `/` | Division* | | Division | `std::ops::Div` | `std::ops::DivAssign` |
211+
| `%` | Remainder | | Remainder | `std::ops::Rem` | `std::ops::RemAssign` |
212+
| `&` | Bitwise AND | [Logical AND] | | `std::ops::BitAnd` | `std::ops::BitAndAssign` |
213+
| <code>&#124;</code> | Bitwise OR | [Logical OR] | | `std::ops::BitOr` | `std::ops::BitOrAssign` |
214+
| `^` | Bitwise XOR | [Logical XOR] | | `std::ops::BitXor` | `std::ops::BitXorAssign` |
215+
| `<<` | Left Shift | | | `std::ops::Shl` | `std::ops::ShlAssign` |
216+
| `>>` | Right Shift** | | | `std::ops::Shr` | `std::ops::ShrAssign` |
217217

218218
\* Integer division rounds towards zero.
219219

@@ -522,6 +522,10 @@ dependency.
522522

523523
[copies or moves]: ../expressions.md#moved-and-copied-types
524524
[dropping]: ../destructors.md
525+
[logical and]: ../types/boolean.md#logical-and
526+
[logical not]: ../types/boolean.md#logical-not
527+
[logical or]: ../types/boolean.md#logical-or
528+
[logical xor]: ../types/boolean.md#logical-xor
525529
[mutable]: ../expressions.md#mutability
526530
[place expression]: ../expressions.md#place-expressions-and-value-expressions
527531
[unit]: ../types/tuple.md

src/types/boolean.md

+66
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,79 @@ Like all primitives, the boolean type [implements][p-impl] the
3232

3333
> **Note**: See the [standard library docs][std] for library operations.
3434
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 &#124; 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
3596
[enumerated type]: enum.md
3697
[expressions]: ../expressions.md
3798
[if expressions]: ../expressions/if-expr.md#if-expressions
3899
[language prelude]: ../names/preludes.md#language-prelude
39100
[lazy]: ../expressions/operator-expr.md#lazy-boolean-operators
40101
[literal expression]: ../expressions/literal-expr.md
41102
[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
42108
[p-clone]: ../special-types-and-traits.md#clone
43109
[p-copy]: ../special-types-and-traits.md#copy
44110
[p-impl]: ../items/implementations.md

0 commit comments

Comments
 (0)