Skip to content

Commit 5037683

Browse files
authored
Merge pull request #940 from Havvy/bool
Referencify bool type
2 parents 64ef976 + 1804726 commit 5037683

File tree

6 files changed

+159
-47
lines changed

6 files changed

+159
-47
lines changed

src/behavior-considered-undefined.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ code.
3838
a function/primitive operation or returned from a function/primitive
3939
operation.
4040
The following values are invalid (at their respective type):
41-
* A value other than `false` (`0`) or `true` (`1`) in a `bool`.
41+
* A value other than `false` (`0`) or `true` (`1`) in a [`bool`].
4242
* A discriminant in an `enum` not included in the type definition.
4343
* A null `fn` pointer.
4444
* A value in a `char` which is a surrogate or above `char::MAX`.
@@ -77,7 +77,8 @@ cannot be bigger than `isize::MAX` bytes.
7777
> vice versa, undefined behavior in Rust can cause adverse affects on code
7878
> executed by any FFI calls to other languages.
7979
80-
[`const`]: items/constant-items.html
80+
[`bool`]: types/boolean.md
81+
[`const`]: items/constant-items.md
8182
[noalias]: http://llvm.org/docs/LangRef.html#noalias
8283
[pointer aliasing rules]: http://llvm.org/docs/LangRef.html#pointer-aliasing-rules
8384
[undef]: http://llvm.org/docs/LangRef.html#undefined-values

src/expressions/if-expr.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
> | _IfExpression_
1111
> | _IfLetExpression_ ) )<sup>\?</sup>
1212
13-
An `if` expression is a conditional branch in program control. The form of an
14-
`if` expression is a condition expression, followed by a consequent block, any
13+
An `if` expression is a conditional branch in program control. The syntax of an
14+
`if` expression is a condition operand, followed by a consequent block, any
1515
number of `else if` conditions and blocks, and an optional trailing `else`
16-
block. The condition expressions must have type `bool`. If a condition
17-
expression evaluates to `true`, the consequent block is executed and any
18-
subsequent `else if` or `else` block is skipped. If a condition expression
16+
block. The condition operands must have the [boolean type]. If a condition
17+
operand evaluates to `true`, the consequent block is executed and any
18+
subsequent `else if` or `else` block is skipped. If a condition operand
1919
evaluates to `false`, the consequent block is skipped and any subsequent `else
2020
if` condition is evaluated. If all `if` and `else if` conditions evaluate to
2121
`false` then any `else` block is executed. An if expression evaluates to the
@@ -52,8 +52,8 @@ assert_eq!(y, "Bigger");
5252
> | _IfLetExpression_ ) )<sup>\?</sup>
5353
5454
An `if let` expression is semantically similar to an `if` expression but in
55-
place of a condition expression it expects the keyword `let` followed by a
56-
pattern, an `=` and a [scrutinee] expression. If the value of the scrutinee
55+
place of a condition operand it expects the keyword `let` followed by a
56+
pattern, an `=` and a [scrutinee] operand. If the value of the scrutinee
5757
matches the pattern, the corresponding block will execute. Otherwise, flow
5858
proceeds to the following `else` block if it exists. Like `if` expressions,
5959
`if let` expressions have a value determined by the block that is evaluated.
@@ -158,4 +158,5 @@ if let PAT = ( EXPR || EXPR ) { .. }
158158
[_MatchArmPatterns_]: match-expr.md
159159
[_eRFCIfLetChain_]: https://github.com/rust-lang/rfcs/blob/master/text/2497-if-let-chains.md#rollout-plan-and-transitioning-to-rust-2018
160160
[`match` expression]: match-expr.md
161+
[boolean type]: ../types/boolean.md
161162
[scrutinee]: ../glossary.md#scrutinee

src/expressions/loop-expr.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ have type compatible with the value of the `break` expression(s).
4747
> _PredicateLoopExpression_ :\
4848
> &nbsp;&nbsp; `while` [_Expression_]<sub>_except struct expression_</sub> [_BlockExpression_]
4949
50-
A `while` loop begins by evaluating the boolean loop conditional expression. If
51-
the loop conditional expression evaluates to `true`, the loop body block
52-
executes, then control returns to the loop conditional expression. If the loop
50+
A `while` loop begins by evaluating the [boolean] loop conditional operand. If
51+
the loop conditional operand evaluates to `true`, the loop body block
52+
executes, then control returns to the loop conditional operand. If the loop
5353
conditional expression evaluates to `false`, the `while` expression completes.
5454

5555
An example:
@@ -294,6 +294,7 @@ expression `()`.
294294
[_MatchArmPatterns_]: match-expr.md
295295
[_Pattern_]: ../patterns.md
296296
[`match` expression]: match-expr.md
297+
[boolean]: ../types/boolean.md
297298
[scrutinee]: ../glossary.md#scrutinee
298299
[temporary values]: ../expressions.md#temporaries
299300
[_LazyBooleanOperatorExpression_]: operator-expr.md#lazy-boolean-operators

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/items/unions.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ let f = unsafe { u.f1 };
4747
Unions have no notion of an "active field". Instead, every union access just
4848
interprets the storage at the type of the field used for the access. Reading a
4949
union field reads the bits of the union at the field's type. Fields might have a
50-
non-zero offset (except when `#[repr(C)]` is used); in that case the bits
51-
starting at the offset of the fields are read. It is the programmer's
50+
non-zero offset (except when [the C representation] is used); in that case the
51+
bits starting at the offset of the fields are read. It is the programmer's
5252
responsibility to make sure that the data is valid at the field's type. Failing
53-
to do so results in undefined behavior. For example, reading the value `3` at
54-
type `bool` is undefined behavior. Effectively, writing to and then reading from
55-
a `#[repr(C)]` union is analogous to a [`transmute`] from the type used for
56-
writing to the type used for reading.
53+
to do so results in [undefined behavior]. For example, reading the value `3`
54+
through of a field of the [boolean type] is undefined behavior. Effectively,
55+
writing to and then reading from a union with [the C representation] is
56+
analogous to a [`transmute`] from the type used for writing to the type used for
57+
reading.
5758

5859
Consequently, all reads of union fields have to be placed in `unsafe` blocks:
5960

@@ -182,4 +183,7 @@ checking, etc etc etc).
182183
[_StructFields_]: structs.md
183184
[`transmute`]: ../../std/mem/fn.transmute.html
184185
[`Copy`]: ../../std/marker/trait.Copy.html
186+
[boolean type]: ../types/boolean.md
185187
[ManuallyDrop]: ../../std/mem/struct.ManuallyDrop.html
188+
[the C representation]: ../type-layout.md#reprc-unions
189+
[undefined behavior]: ../behavior-considered-undefined.html

src/types/boolean.md

+114-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,118 @@
11
# Boolean type
22

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-
73
```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;
175
```
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 &#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
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

Comments
 (0)