]> git.proxmox.com Git - rustc.git/blame - src/doc/reference/src/expressions/tuple-expr.md
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / doc / reference / src / expressions / tuple-expr.md
CommitLineData
ea8adc8c
XL
1# Tuple and tuple indexing expressions
2
3## Tuple expressions
4
8faf50e0
XL
5> **<sup>Syntax</sup>**\
6> _TupleExpression_ :\
13cf67c4
XL
7> &nbsp;&nbsp; `(` [_InnerAttribute_]<sup>\*</sup> _TupleElements_<sup>?</sup> `)`
8>
9> _TupleElements_ :\
10> &nbsp;&nbsp; ( [_Expression_] `,` )<sup>+</sup> [_Expression_]<sup>?</sup>
0531ce1d 11
6a06907d 12Tuple expressions evaluate into [tuple values][tuple type] with the operands initializing the elements of the tuple.
ea8adc8c 13
6a06907d 14Tuple 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].
ea8adc8c 15
6a06907d
XL
16The number of operands is the arity of the constructed tuple.
17Tuple expressions without operands produce the unit tuple.
18For other tuple expressions, the first written operand initializes the 0th element and subsequent operands initializes the next highest element.
19For example, in the tuple expression `('a', 'b', 'c')`, `'a'` initializes the value of the 0th element, `'b'` the 1st, and `'c'` the 2nd.
ea8adc8c 20
29967ef6
XL
21Examples of tuple expressions:
22
23| Expression | Type |
24| -------------------- | ------------ |
25| `()` | `()` (unit) |
26| `(0.0, 4.5)` | `(f64, f64)` |
27| `("x".to_string(), )` | `(String, )` |
28| `("a", 4usize, true)`| `(&'static str, usize, bool)` |
ea8adc8c 29
13cf67c4
XL
30### Tuple expression attributes
31
6a06907d 32[Inner attributes] are allowed directly after the opening parenthesis of a tuple expression in the same expression contexts as [attributes on block expressions].
13cf67c4 33
ea8adc8c
XL
34## Tuple indexing expressions
35
8faf50e0
XL
36> **<sup>Syntax</sup>**\
37> _TupleIndexingExpression_ :\
0531ce1d
XL
38> &nbsp;&nbsp; [_Expression_] `.` [TUPLE_INDEX]
39
6a06907d 40Tuple indexing expressions evaluate like [field access expressions], but access elements of [tuples][tuple type] or [tuple structs].
29967ef6 41
6a06907d
XL
42Tuple index expressions are written as an operand, `.`, and a tuple index.
43The index must be written as a [decimal literal] with no leading zeros, underscores, or suffix.
44The operand must have the type of a tuple or tuple struct.
45If the tuple index is not an element of the tuple or tuple struct, it is a compiler error.
29967ef6
XL
46
47Examples of tuple indexing expressions:
ea8adc8c
XL
48
49```rust
29967ef6 50let pair = ("a string", 2);
ea8adc8c 51assert_eq!(pair.1, 2);
29967ef6
XL
52
53# struct Point(f32, f32);
54let point = Point(1.0, 0.0);
55assert_eq!(point.0, 1.0);
56assert_eq!(point.1, 0.0);
ea8adc8c 57```
0531ce1d 58
6a06907d 59> **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.
29967ef6 60
416331ca
XL
61[_Expression_]: ../expressions.md
62[_InnerAttribute_]: ../attributes.md
63[attributes on block expressions]: block-expr.md#attributes-on-block-expressions
29967ef6
XL
64[call expression]: ./call-expr.md
65[decimal literal]: ../tokens.md#integer-literals
66[field access expressions]: ./field-expr.html#field-access-expressions
67[Inner attributes]: ../attributes.md
68[operands]: ../expressions.md
69[parenthetical expression]: grouped-expr.md
70[tuple type]: ../types/tuple.md
71[tuple structs]: ../types/struct.md
72[TUPLE_INDEX]: ../tokens.md#tuple-index