Skip to content

Commit c164fb7

Browse files
authored
Merge pull request #990 from Havvy/tuple-index-examples
Tuple Passover #2
2 parents c40d712 + 09142b8 commit c164fb7

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

src/expressions/tuple-expr.md

+29-14
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@
99
> _TupleElements_ :\
1010
> &nbsp;&nbsp; ( [_Expression_] `,` )<sup>+</sup> [_Expression_]<sup>?</sup>
1111
12-
Tuple expressions evaluate into [tuple values][tuple type] with the operands initializing the elements of the tuple.
12+
A *tuple expression* constructs [tuple values][tuple type].
1313

14-
Tuple expressions are written by listing the [operands] in a parenthesized, comma-separated list. 1-ary tuple expressions require a comma after their operand to be disambiguated with a [parenthetical expression].
14+
The syntax for tuple expressions is a parenthesized, comma separated list of expressions, called the *tuple initializer operands*.
15+
1-ary tuple expressions require a comma after their tuple initializer operand to be disambiguated with a [parenthetical expression].
1516

16-
The number of operands is the arity of the constructed tuple.
17-
Tuple expressions without operands produce the unit tuple.
18-
For other tuple expressions, the first written operand initializes the 0th element and subsequent operands initializes the next highest element.
19-
For example, in the tuple expression `('a', 'b', 'c')`, `'a'` initializes the value of the 0th element, `'b'` the 1st, and `'c'` the 2nd.
17+
Tuple expressions are a [value expression] that evaluate into a newly constructed value of a tuple type.
18+
The number of tuple initializer operands is the arity of the constructed tuple.
19+
Tuple expressions without any tuple initializer operands produce the unit tuple.
20+
For other tuple expressions, the first written tuple initializer operand initializes the field `0` and subsequent operands initializes the next highest field.
21+
For example, in the tuple expression `('a', 'b', 'c')`, `'a'` initializes the value of the field `0`, `'b'` field `1`, and `'c'` field `2`.
2022

21-
Examples of tuple expressions:
23+
Examples of tuple expressions and their types:
2224

2325
| Expression | Type |
2426
| -------------------- | ------------ |
@@ -37,19 +39,26 @@ Examples of tuple expressions:
3739
> _TupleIndexingExpression_ :\
3840
> &nbsp;&nbsp; [_Expression_] `.` [TUPLE_INDEX]
3941
40-
Tuple indexing expressions evaluate like [field access expressions], but access elements of [tuples][tuple type] or [tuple structs].
42+
A *tuple indexing expression* accesses fields of [tuples][tuple type] and [tuple structs][tuple struct].
4143

42-
Tuple index expressions are written as an operand, `.`, and a tuple index.
43-
The index must be written as a [decimal literal] with no leading zeros, underscores, or suffix.
44-
The operand must have the type of a tuple or tuple struct.
45-
If the tuple index is not an element of the tuple or tuple struct, it is a compiler error.
44+
The syntax for a tuple index expression is an expression, called the *tuple operand*, then a `.`, then finally a tuple index.
45+
The syntax for the *tuple index* is a [decimal literal] with no leading zeros, underscores, or suffix.
46+
For example `0` and `2` are valid tuple indices but not `01`, `0_`, nor `0i32`.
47+
48+
The type of the tuple operand must be a [tuple type] or a [tuple struct].
49+
The tuple index must be a name of a field of the type of the tuple operand.
50+
51+
Evaluation of tuple index expressions has no side effects beyond evaluation of its tuple operand.
52+
As a [place expression], it evaluates to the location of the field of the tuple operand with the same name as the tuple index.
4653

4754
Examples of tuple indexing expressions:
4855

4956
```rust
57+
// Indexing a tuple
5058
let pair = ("a string", 2);
5159
assert_eq!(pair.1, 2);
5260

61+
// Indexing a tuple struct
5362
# struct Point(f32, f32);
5463
let point = Point(1.0, 0.0);
5564
assert_eq!(point.0, 1.0);
@@ -58,15 +67,21 @@ assert_eq!(point.1, 0.0);
5867

5968
> **Note**: Unlike field access expressions, tuple index expressions can be the function operand of a [call expression] as it cannot be confused with a method call since method names cannot be numbers.
6069
70+
> **Note**: Although arrays and slices also have elements, you must use an [array or slice indexing expression] or a [slice pattern] to access their elements.
71+
6172
[_Expression_]: ../expressions.md
6273
[_InnerAttribute_]: ../attributes.md
74+
[array or slice indexing expression]: array-expr.md#array-and-slice-indexing-expressions
6375
[attributes on block expressions]: block-expr.md#attributes-on-block-expressions
6476
[call expression]: ./call-expr.md
6577
[decimal literal]: ../tokens.md#integer-literals
6678
[field access expressions]: ./field-expr.html#field-access-expressions
67-
[Inner attributes]: ../attributes.md
79+
[inner attributes]: ../attributes.md
6880
[operands]: ../expressions.md
6981
[parenthetical expression]: grouped-expr.md
82+
[place expression]: ../expressions.md#place-expressions-and-value-expressions
83+
[slice pattern]: ../patterns.md#slice-patterns
7084
[tuple type]: ../types/tuple.md
71-
[tuple structs]: ../types/struct.md
85+
[tuple struct]: ../types/struct.md
7286
[TUPLE_INDEX]: ../tokens.md#tuple-index
87+
[value expression]: ../expressions.md#place-expressions-and-value-expressions

src/types/tuple.md

+20-21
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@
55
> &nbsp;&nbsp; &nbsp;&nbsp; `(` `)`\
66
> &nbsp;&nbsp; | `(` ( [_Type_] `,` )<sup>+</sup> [_Type_]<sup>?</sup> `)`
77
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.
8+
*Tuple types* are a family of structural types[^1] for heterogeneous lists of other types.
119

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.
10+
The syntax for a tuple type is a parenthesized, comma-separated list of types.
11+
1-ary tuples require a comma after their element type to be disambiguated with a [parenthesized type].
1512

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*.
13+
A tuple type has a number of fields equal to the length of the list of types.
14+
This number of fields determines the *arity* of the tuple.
15+
A tuple with `n` fields is called an *n-ary tuple*.
16+
For example, a tuple with 2 fields is a 2-ary tuple.
1917

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].
18+
Fields of tuples are named using increasing numeric names matching their position in the list of types.
19+
The first field is `0`.
20+
The second field is `1`.
21+
And so on.
22+
The type of each field is the type of the same position in the tuple's list of types.
23+
24+
For convenience and historical reasons, the tuple type with no fields (`()`) is often called *unit* or *the unit type*.
25+
It's one value is also called *unit* or *the unit value*.
2326

2427
Some examples of tuple types:
2528

@@ -29,16 +32,12 @@ Some examples of tuple types:
2932
* `(i32, String)` (different type from the previous example)
3033
* `(i32, f64, Vec<String>, Option<bool>)`
3134

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].
35+
Values of this type are constructed using a [tuple expression].
36+
Furthermore, various expressions will produce the unit value if there is no other meaningful value for it to evaluate to.
37+
Tuple fields can be accessed by either a [tuple index expression] or [pattern matching].
3938

40-
[^2]: Element is equivalent to field, except numerical indexes instead of
41-
identifiers
39+
[^1]: Structural types are always equivalent if their internal types are equivalent.
40+
For a nominal version of tuples, see [tuple structs].
4241

4342
[_Type_]: ../types.md#type-expressions
4443
[parenthesized type]: ../types.md#parenthesized-types

0 commit comments

Comments
 (0)