Skip to content

Commit

Permalink
Document expression order for tuple expressions.
Browse files Browse the repository at this point in the history
Also removes a "you", alphabetizes the link refs, links to the
disambiguated parenthetical expression, and moves a link to a link
ref.

I probably should have made multiple commits. Sorry.
  • Loading branch information
Havvy committed Oct 4, 2020
1 parent 56a13c0 commit 7d52435
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/expressions/tuple-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@
> &nbsp;&nbsp; ( [_Expression_] `,` )<sup>+</sup> [_Expression_]<sup>?</sup>
Tuples are written by enclosing zero or more comma-separated expressions in
parentheses. They are used to create [tuple-typed](../types/tuple.md)
values.
parentheses. They are used to create [tuple-typed] values.

```rust
(0.0, 4.5);
("a", 4usize, true);
();
```

You can disambiguate a single-element tuple from a value in parentheses with a
comma:
The inner expressions are evaluated from left to right.

```rust
# // Using vec instead of array to avoid references
# // since there is no stable owned array iterator
# // at the time this example was written.
let mut one_two = vec![1, 2].into_iter();
assert_eq!(
(1, 2),
(one_two.next().unwrap(), one_two.next().unwrap())
);
```

Single-element tuples are disambiguated from a [parenthetical expression] with a
comma before the closing parethesis:

```rust
(0,); // single-element tuple
Expand Down Expand Up @@ -54,8 +66,10 @@ let unit_x = Point(1.0, 0.0);
assert_eq!(unit_x.0, 1.0);
```

[Inner attributes]: ../attributes.md
[TUPLE_INDEX]: ../tokens.md#tuple-index
[_Expression_]: ../expressions.md
[_InnerAttribute_]: ../attributes.md
[attributes on block expressions]: block-expr.md#attributes-on-block-expressions
[Inner attributes]: ../attributes.md
[parenthetical expression]: grouped-expr.md
[tuple-typed]: ../types/tuple.md
[TUPLE_INDEX]: ../tokens.md#tuple-index

0 comments on commit 7d52435

Please sign in to comment.