Skip to content

Commit e0072e3

Browse files
committed
doc: structure expressions. cc: #4217
1 parent 4676697 commit e0072e3

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

doc/rust.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,39 @@ values.
15031503
("a", 4u, true);
15041504
~~~~~~~~
15051505

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+
15061539
### Record expressions
15071540

15081541
~~~~~~~~{.ebnf .gram}
@@ -1511,9 +1544,11 @@ rec_expr : '{' ident ':' expr
15111544
[ ".." expr ] '}'
15121545
~~~~~~~~
15131546

1547+
> **Note:** In future versions of Rust, record expressions and [record types](#record-types) will be removed.
1548+
15141549
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
15171552
colon. To indicate that a field is mutable, the `mut` keyword is
15181553
written before its name.
15191554

0 commit comments

Comments
 (0)