99> _ TupleElements_ :\
1010>   ;  ; ( [ _ 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] [ tuple type ] with the operands
13+ initializing 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 [ parenthetical 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,40 @@ expressions].
3944> _ TupleIndexingExpression_ :\
4045>   ;  ; [ _ 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 );
5260assert_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+ [ decimal literal ] : ../tokens.md#integer-literals
77+ [ field access expressions ] : ./field-expr.html#field-access-expressions
78+ [ Inner attributes ] : ../attributes.md
79+ [ operands ] : ../expressions.md
80+ [ parenthetical expression ] : grouped-expr.md
81+ [ tuple type ] : ../types/tuple.md
82+ [ tuple structs ] : ../types/struct.md
83+ [ TUPLE_INDEX ] : ../tokens.md#tuple-index
0 commit comments