You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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].
13
13
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].
15
16
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`.
20
22
21
-
Examples of tuple expressions:
23
+
Examples of tuple expressions and their types:
22
24
23
25
| Expression | Type |
24
26
| -------------------- | ------------ |
@@ -37,19 +39,26 @@ Examples of tuple expressions:
37
39
> _TupleIndexingExpression_ :\
38
40
> [_Expression_]`.`[TUPLE_INDEX]
39
41
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].
41
43
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.
46
53
47
54
Examples of tuple indexing expressions:
48
55
49
56
```rust
57
+
// Indexing a tuple
50
58
letpair= ("a string", 2);
51
59
assert_eq!(pair.1, 2);
52
60
61
+
// Indexing a tuple struct
53
62
# structPoint(f32, f32);
54
63
letpoint=Point(1.0, 0.0);
55
64
assert_eq!(point.0, 1.0);
@@ -58,15 +67,21 @@ assert_eq!(point.1, 0.0);
58
67
59
68
> **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.
60
69
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
+
61
72
[_Expression_]: ../expressions.md
62
73
[_InnerAttribute_]: ../attributes.md
74
+
[array or slice indexing expression]: array-expr.md#array-and-slice-indexing-expressions
63
75
[attributes on block expressions]: block-expr.md#attributes-on-block-expressions
0 commit comments