@@ -1503,6 +1503,39 @@ values.
1503
1503
("a", 4u, true);
1504
1504
~~~~~~~~
1505
1505
1506
+ ### Structure expressions
1507
+
1508
+ ~~~~~~~~ {.ebnf .gram}
1509
+ struct_expr : expr_path '{' ident ':' expr
1510
+ [ ',' ident ':' expr ] *
1511
+ [ ".." expr ] '}'
1512
+ ~~~~~~~~
1513
+
1514
+ A _ structure expression_ consists of the [ path] ( #paths ) of a [ structure item] ( #structures ) ,
1515
+ followed by a brace-enclosed list of one or more comma-separated name-value pairs,
1516
+ providing the field values of a new instance of the structure.
1517
+ A field name can be any identifier, and is separated from its value expression by a colon.
1518
+ To indicate that a field is mutable, the ` mut ` keyword is written before its name.
1519
+
1520
+ The following are examples of structure expressions:
1521
+
1522
+ ~~~~
1523
+ Point {x: 10f, y: 20f};
1524
+ game::User {name: "Joe", age: 35u, mut score: 100_000};
1525
+ ~~~~
1526
+
1527
+ A structure expression forms a new value of the named structure type.
1528
+
1529
+ A structure expression can terminate with the syntax ` .. ` followed by an expression to denote a functional update.
1530
+ The expression following ` .. ` (the base) must be of the same structure type as the new structure type being formed.
1531
+ A new structure will be created, of the same type as the base expression, with the given values for the fields that were explicitly specified,
1532
+ and the values in the base record for all other fields.
1533
+
1534
+ ~~~~
1535
+ let base = Point3d {x: 1, y: 2, z: 3};
1536
+ Point3d {y: 0, z: 10, .. base};
1537
+ ~~~~
1538
+
1506
1539
### Record expressions
1507
1540
1508
1541
~~~~~~~~ {.ebnf .gram}
@@ -1511,9 +1544,11 @@ rec_expr : '{' ident ':' expr
1511
1544
[ ".." expr ] '}'
1512
1545
~~~~~~~~
1513
1546
1547
+ > ** Note:** In future versions of Rust, record expressions and [ record types] ( #record-types ) will be removed.
1548
+
1514
1549
A [ _ record_ ] ( #record-types ) _ expression_ is one or more comma-separated
1515
- name-value pairs enclosed by braces. A fieldname can be any identifier
1516
- (including keywords), and is separated from its value expression by a
1550
+ name-value pairs enclosed by braces. A fieldname can be any identifier,
1551
+ and is separated from its value expression by a
1517
1552
colon. To indicate that a field is mutable, the ` mut ` keyword is
1518
1553
written before its name.
1519
1554
0 commit comments