Skip to content

Commit 0917e13

Browse files
committed
auto merge of #5187 : ben0x539/rust/docs-unit-struct, r=catamorphism
This adds a few words about unit-like struct types (`struct Foo;`) in the sections for `struct` items, structure expressions and structure types (and fixes an adjacent typo or two). The added text is at the same time triply redundant because of how the sections are split and rather brief because I don't think there's that much to say about field-less structs without digressing into `impl`s and generic functions and whatnot, but it's probably better than nothing for a start. The added arm for the grammar of struct expressions is really awkward. It's just | expr_path which is clearly not unambiguously a struct expression, but it didn't feel right not to add anything to the grammar chunk (and I can't tell whether the arm for enum-like structs is somehow unambiguous with regular enum expressions, either). Is this okay?
2 parents 3f91f32 + 332c046 commit 0917e13

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

doc/rust.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,15 @@ let p = Point(10, 11);
10851085
let px: int = match p { Point(x, _) => x };
10861086
~~~~
10871087

1088+
A _unit-like struct_ is a structure without any fields, defined by leaving off the fields list entirely.
1089+
Such types will have a single value, just like the [unit value `()`](#unit-and-boolean-literals) of the unit type.
1090+
For example:
1091+
1092+
~~~~
1093+
struct Cookie;
1094+
let c = [Cookie, Cookie, Cookie, Cookie];
1095+
~~~~
1096+
10881097
### Enumerations
10891098

10901099
An _enumeration_ is a simultaneous definition of a nominal [enumerated type](#enumerated-types) as well as a set of *constructors*,
@@ -1590,7 +1599,8 @@ struct_expr : expr_path '{' ident ':' expr
15901599
[ ',' ident ':' expr ] *
15911600
[ ".." expr ] '}' |
15921601
expr_path '(' expr
1593-
[ ',' expr ] * ')'
1602+
[ ',' expr ] * ')' |
1603+
expr_path
15941604
~~~~~~~~
15951605

15961606
There are several forms of structure expressions.
@@ -1600,23 +1610,28 @@ providing the field values of a new instance of the structure.
16001610
A field name can be any identifier, and is separated from its value expression by a colon.
16011611
To indicate that a field is mutable, the `mut` keyword is written before its name.
16021612

1603-
A _tuple structure expression_ constists of the [path](#paths) of a [structure item](#structures),
1613+
A _tuple structure expression_ consists of the [path](#paths) of a [structure item](#structures),
16041614
followed by a parenthesized list of one or more comma-separated expressions
16051615
(in other words, the path of a structured item followed by a tuple expression).
16061616
The structure item must be a tuple structure item.
16071617

1618+
A _unit-like structure expression_ consists only of the [path](#paths) of a [structure item](#structures).
1619+
16081620
The following are examples of structure expressions:
16091621

16101622
~~~~
16111623
# struct Point { x: float, y: float }
16121624
# struct TuplePoint(float, float);
16131625
# mod game { pub struct User { name: &str, age: uint, score: uint } }
1626+
# struct Cookie; fn some_fn<T>(t: T) {}
16141627
Point {x: 10f, y: 20f};
16151628
TuplePoint(10f, 20f);
16161629
let u = game::User {name: "Joe", age: 35u, score: 100_000};
1630+
some_fn::<Cookie>(Cookie);
16171631
~~~~
16181632

16191633
A structure expression forms a new value of the named structure type.
1634+
Note that for a given *unit-like* structure type, this will always be the same value.
16201635

16211636
A structure expression can terminate with the syntax `..` followed by an expression to denote a functional update.
16221637
The expression following `..` (the base) must be of the same structure type as the new structure type being formed.
@@ -2643,7 +2658,10 @@ the resulting `struct` value will always be laid out in memory in the order spec
26432658
The fields of a `struct` may be qualified by [visibility modifiers](#visibility-modifiers),
26442659
to restrict access to implementation-private data in a structure.
26452660

2646-
A `tuple struct` type is just like a structure type, except that the fields are anonymous.
2661+
A _tuple struct_ type is just like a structure type, except that the fields are anonymous.
2662+
2663+
A _unit-like struct_ type is like a structure type, except that it has no fields.
2664+
The one value constructed by the associated [structure expression](#structure-expression) is the only value that inhabits such a type.
26472665

26482666
### Enumerated types
26492667

0 commit comments

Comments
 (0)