@@ -1072,6 +1072,15 @@ let p = Point {x: 10, y: 11};
1072
1072
let px: int = p.x;
1073
1073
~~~~
1074
1074
1075
+ A _ tuple structure_ is a nominal [ tuple type] ( #tuple-types ) , also defined with the keyword ` struct ` .
1076
+ For example:
1077
+
1078
+ ~~~~
1079
+ struct Point(int, int);
1080
+ let p = Point(10, 11);
1081
+ let px: int = match p { Point(x, _) => x };
1082
+ ~~~~
1083
+
1075
1084
### Enumerations
1076
1085
1077
1086
An _ enumeration_ is a simultaneous definition of a nominal [ enumerated type] ( #enumerated-types ) as well as a set of * constructors* ,
@@ -1534,22 +1543,32 @@ values.
1534
1543
~~~~~~~~ {.ebnf .gram}
1535
1544
struct_expr : expr_path '{' ident ':' expr
1536
1545
[ ',' ident ':' expr ] *
1537
- [ ".." expr ] '}'
1546
+ [ ".." expr ] '}' |
1547
+ expr_path '(' expr
1548
+ [ ',' expr ] * ')'
1538
1549
~~~~~~~~
1539
1550
1551
+ There are several forms of structure expressions.
1540
1552
A _ structure expression_ consists of the [ path] ( #paths ) of a [ structure item] ( #structures ) ,
1541
1553
followed by a brace-enclosed list of one or more comma-separated name-value pairs,
1542
1554
providing the field values of a new instance of the structure.
1543
1555
A field name can be any identifier, and is separated from its value expression by a colon.
1544
1556
To indicate that a field is mutable, the ` mut ` keyword is written before its name.
1545
1557
1558
+ A _ tuple structure expression_ constists of the [ path] ( #paths ) of a [ structure item] ( #structures ) ,
1559
+ followed by a parenthesized list of one or more comma-separated expressions
1560
+ (in other words, the path of a structured item followed by a tuple expression).
1561
+ The structure item must be a tuple structure item.
1562
+
1546
1563
The following are examples of structure expressions:
1547
1564
1548
1565
~~~~
1549
1566
# struct Point { x: float, y: float }
1567
+ # struct TuplePoint(float, float);
1550
1568
# mod game { pub struct User { name: &str, age: uint, mut score: uint } }
1551
1569
# use game;
1552
1570
Point {x: 10f, y: 20f};
1571
+ TuplePoint(10f, 20f);
1553
1572
let u = game::User {name: "Joe", age: 35u, mut score: 100_000};
1554
1573
~~~~
1555
1574
@@ -2597,6 +2616,7 @@ the resulting `struct` value will always be laid out in memory in the order spec
2597
2616
The fields of a ` struct ` may be qualified by [ visibility modifiers] ( #visibility-modifiers ) ,
2598
2617
to restrict access to implementation-private data in a structure.
2599
2618
2619
+ A ` tuple struct ` type is just like a structure type, except that the fields are anonymous.
2600
2620
2601
2621
### Enumerated types
2602
2622
0 commit comments