|
10 | 10 | > ( [_Expression_] `,` )<sup>+</sup> [_Expression_]<sup>?</sup>
|
11 | 11 |
|
12 | 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. |
| 13 | +parentheses. They are used to create [tuple-typed] values. |
15 | 14 |
|
16 | 15 | ```rust
|
17 | 16 | (0.0, 4.5);
|
18 | 17 | ("a", 4usize, true);
|
19 | 18 | ();
|
20 | 19 | ```
|
21 | 20 |
|
22 |
| -You can disambiguate a single-element tuple from a value in parentheses with a |
23 |
| -comma: |
| 21 | +The inner expressions are evaluated from left to right. |
| 22 | + |
| 23 | +```rust |
| 24 | +#// Using vec instead of array to avoid references |
| 25 | +#// since there is no stable owned array iterator |
| 26 | +#// at the time this example was written. |
| 27 | +let mut one_two = vec![1, 2].into_iter(); |
| 28 | +assert_eq!( |
| 29 | + (1, 2), |
| 30 | + (one_two.next().unwrap(), one_two.next().unwrap()) |
| 31 | +); |
| 32 | +``` |
| 33 | + |
| 34 | +Single-element tuples are disambiguated from a [parenthetical expression] with a |
| 35 | +comma before the closing parethesis: |
24 | 36 |
|
25 | 37 | ```rust
|
26 | 38 | (0,); // single-element tuple
|
@@ -54,8 +66,10 @@ let unit_x = Point(1.0, 0.0);
|
54 | 66 | assert_eq!(unit_x.0, 1.0);
|
55 | 67 | ```
|
56 | 68 |
|
57 |
| -[Inner attributes]: ../attributes.md |
58 |
| -[TUPLE_INDEX]: ../tokens.md#tuple-index |
59 | 69 | [_Expression_]: ../expressions.md
|
60 | 70 | [_InnerAttribute_]: ../attributes.md
|
61 | 71 | [attributes on block expressions]: block-expr.md#attributes-on-block-expressions
|
| 72 | +[Inner attributes]: ../attributes.md |
| 73 | +[parenthetical expression]: grouped-expr.md |
| 74 | +[tuple-typed]: ../types/tuple.md |
| 75 | +[TUPLE_INDEX]: ../tokens.md#tuple-index |
0 commit comments