Skip to content

Commit c7b1801

Browse files
committed
Referencify tuples
I'm not sure "initialized" is the correct word, but I feel like our verbiage around place and value expressions need to be cleaned up anyways. I originally went with "moved", but that's not really correct. I think I want to call it a "value operand", but I need to discuss with others on that first. In any case, it's a lot more correct than what was there before. I renamed `unit_x` to `point` in an example so as to not cause confusion with the unit tuple.
1 parent 56a13c0 commit c7b1801

File tree

3 files changed

+89
-45
lines changed

3 files changed

+89
-45
lines changed

src/expressions/tuple-expr.md

+47-26
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,28 @@
99
> _TupleElements_ :\
1010
> &nbsp;&nbsp; ( [_Expression_] `,` )<sup>+</sup> [_Expression_]<sup>?</sup>
1111
12-
Tuples are written by enclosing zero or more comma-separated expressions in
13-
parentheses. They are used to create [tuple-typed](../types/tuple.md)
14-
values.
12+
Tuple expressions evaluate into [tuple values] with the operands initializing
13+
the elements of the tuple.
1514

16-
```rust
17-
(0.0, 4.5);
18-
("a", 4usize, true);
19-
();
20-
```
15+
Tuple expressions are written by listing the [operands] in a parenthesized,
16+
comma-separated list. 1-ary tuple expressions require a comma after their
17+
operand to be disambiguated with a [parenthesized expression].
2118

22-
You can disambiguate a single-element tuple from a value in parentheses with a
23-
comma:
19+
The number of operands is the arity of the constructed tuple. Tuple expressions
20+
without operands produce the unit tuple. For other tuple expressions, the first
21+
written operand initializes the 0th element and subsequent operands initializes
22+
the next highest element. For example, in the tuple expression
23+
`('a', 'b', 'c')`, `'a'` initializes the value of the 0th element, `'b'` the
24+
1st, and `'c'` the 2nd.
2425

25-
```rust
26-
(0,); // single-element tuple
27-
(0); // zero in parentheses
28-
```
26+
Examples of tuple expressions:
27+
28+
| Expression | Type |
29+
| -------------------- | ------------ |
30+
| `()` | `()` (unit) |
31+
| `(0.0, 4.5)` | `(f64, f64)` |
32+
| `("x".to_string(), ) | `(String, ) |
33+
| `("a", 4usize, true)`| `(&'static str, usize, bool)` |
2934

3035
### Tuple expression attributes
3136

@@ -39,23 +44,39 @@ expressions].
3944
> _TupleIndexingExpression_ :\
4045
> &nbsp;&nbsp; [_Expression_] `.` [TUPLE_INDEX]
4146
42-
[Tuples](../types/tuple.md) and [struct tuples](../items/structs.md) can be
43-
indexed using the number corresponding to the position of the field. The index
44-
must be written as a [decimal literal](../tokens.md#integer-literals) with no
45-
underscores or suffix. Tuple indexing expressions also differ from field
46-
expressions in that they can unambiguously be called as a function. In all
47-
other aspects they have the same behavior.
47+
Tuple indexing expressions evaluate like [field access expressions], but access
48+
elements of [tuples][tuple type] or [tuple structs].
49+
50+
Tuple index expressions are written as an operand, `.`, and a tuple index. The
51+
index must be written as a [decimal literal] with no leading zeros, underscores,
52+
or suffix. The operand must have the type of a tuple or tuple struct. If the
53+
tuple index is not an element of the tuple or tuple struct, it is a compiler
54+
error.
55+
56+
Examples of tuple indexing expressions:
4857

4958
```rust
50-
# struct Point(f32, f32);
51-
let pair = (1, 2);
59+
let pair = ("a string", 2);
5260
assert_eq!(pair.1, 2);
53-
let unit_x = Point(1.0, 0.0);
54-
assert_eq!(unit_x.0, 1.0);
61+
62+
# struct Point(f32, f32);
63+
let point = Point(1.0, 0.0);
64+
assert_eq!(point.0, 1.0);
65+
assert_eq!(point.1, 0.0);
5566
```
5667

57-
[Inner attributes]: ../attributes.md
58-
[TUPLE_INDEX]: ../tokens.md#tuple-index
68+
> **Note**: Unlike field access expressions, tuple index expressions can be the
69+
> function operand of a [call expression] as it cannot be confused with a
70+
> method call since method names cannot be numbers.
71+
5972
[_Expression_]: ../expressions.md
6073
[_InnerAttribute_]: ../attributes.md
6174
[attributes on block expressions]: block-expr.md#attributes-on-block-expressions
75+
[call expression]: ./call-expr.md
76+
[field access expressions]: ./field-expr.html#field-access-expressions
77+
[Inner attributes]: ../attributes.md
78+
[operands]: ../expressions.md
79+
[parenthetical expression]: grouped-expr.md
80+
[tuple type]: ../types/tuple.md
81+
[tuple structs]: ../types/struct.md
82+
[TUPLE_INDEX]: ../tokens.md#tuple-index

src/patterns.md

+10
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,16 @@ require a comma, and matches a tuple of any size.
649649

650650
The tuple pattern is refutable when one of its subpatterns is refutable.
651651

652+
An example of using tuple patterns:
653+
654+
```rust
655+
let pair = (10, "ten");
656+
let (a, b) = pair;
657+
658+
assert_eq!(a, 10);
659+
assert_eq!(b, "ten");
660+
```
661+
652662
## Grouped patterns
653663

654664
> **<sup>Syntax</sup>**\

src/types/tuple.md

+32-19
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,43 @@
55
> &nbsp;&nbsp; &nbsp;&nbsp; `(` `)`\
66
> &nbsp;&nbsp; | `(` ( [_Type_] `,` )<sup>+</sup> [_Type_]<sup>?</sup> `)`
77
8-
A tuple *type* is a heterogeneous product of other types, called the *elements*
9-
of the tuple. It has no nominal name and is instead structurally typed.
8+
The *tuple type* is a structural type[^1] for heterogeneous lists of other
9+
types. Each entry in the list is an *element*[^2] of the tuple. The position of
10+
the element makes it the *nth element* using zero (`0`) as the initial index.
1011

11-
Tuple types and values are denoted by listing the types or values of their
12-
elements, respectively, in a parenthesized, comma-separated list.
12+
The number of elements determines the arity of the tuple. A tuple with `n`
13+
elements is called an `n-ary tuple`. For example, a tuple with 2 elements is a
14+
2-ary tuple.
1315

14-
Because tuple elements don't have a name, they can only be accessed by
15-
pattern-matching or by using `N` directly as a field to access the `N`th
16-
element.
16+
For convenience and historical reasons, the tuple type with no elements (`()`)
17+
is often called *unit* or *the unit type*. It's one value is also called *unit*
18+
or *the unit value*.
1719

18-
An example of a tuple type and its use:
20+
Tuple types are written by listing the types of their elements in a
21+
parenthesized, comma-separated list. 1-ary tuples require a comma after their
22+
element type to be disambiguated with a [parenthesized type].
1923

20-
```rust
21-
type Pair<'a> = (i32, &'a str);
22-
let p: Pair<'static> = (10, "ten");
23-
let (a, b) = p;
24+
Some examples of tuple types:
2425

25-
assert_eq!(a, 10);
26-
assert_eq!(b, "ten");
27-
assert_eq!(p.0, 10);
28-
assert_eq!(p.1, "ten");
29-
```
26+
* `()` (unit)
27+
* `(f64, f64)`
28+
* `(String, i32)`
29+
* `(i32, String)` (different type from the previous example)
30+
* `(i32, f64, Vec<String>, Option<bool>)`
3031

31-
For historical reasons and convenience, the tuple type with no elements (`()`)
32-
is often called ‘unit’ or ‘the unit type’.
32+
Values of this type are constructed using a [tuple expression]. Furthermore,
33+
various expressions will produce the unit value if there is no other meaningful
34+
value for it to evaluate to. Tuple elements can be accessed by either a [tuple
35+
index expression] or [pattern matching].
36+
37+
[^1]: Structural types are always equivalent if their internal types are
38+
equivalent. For a nominal version of tuples, see [tuple structs].
39+
[^2]: Element is equivalent to field, except numerical indexes instead of
40+
identifiers
3341

3442
[_Type_]: ../types.md#type-expressions
43+
[parenthesized type]: ../types.md#parenthesized-types
44+
[pattern matching]: ../patterns.md#tuple-patterns
45+
[tuple expression]: ../expressions/tuple-expr.md#tuple-expressions
46+
[tuple index expression]: ../expressions/tuple-expr.md#tuple-indexing-expressions
47+
[tuple structs]: ./struct.md

0 commit comments

Comments
 (0)