9
9
> _ TupleElements_ :\
10
10
>   ;  ; ( [ _ Expression_ ] ` , ` )<sup >+</sup > [ _ Expression_ ] <sup >?</sup >
11
11
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.
15
14
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] .
21
18
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.
24
25
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) ` |
29
34
30
35
### Tuple expression attributes
31
36
@@ -39,23 +44,39 @@ expressions].
39
44
> _ TupleIndexingExpression_ :\
40
45
>   ;  ; [ _ Expression_ ] ` . ` [ TUPLE_INDEX]
41
46
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:
48
57
49
58
``` rust
50
- # struct Point (f32 , f32 );
51
- let pair = (1 , 2 );
59
+ let pair = (" a string" , 2 );
52
60
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 );
55
66
```
56
67
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
+
59
72
[ _Expression_ ] : ../expressions.md
60
73
[ _InnerAttribute_ ] : ../attributes.md
61
74
[ 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
0 commit comments