Skip to content

Commit 48ad733

Browse files
authored
Merge pull request #1799 from WaffleLapkin/qualified_unit
Improve documentation of struct expressions
2 parents e72ac2a + f025b98 commit 48ad733

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

src/expressions/struct-expr.md

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ StructExprField ->
2323
2424
StructBase -> `..` Expression
2525
26-
StructExprTuple ->
27-
PathInExpression `(`
28-
( Expression (`,` Expression)* `,`? )?
29-
`)`
26+
StructExprTuple -> CallExpression
3027
31-
StructExprUnit -> PathInExpression
28+
StructExprUnit -> PathExpression
3229
```
3330

3431
r[expr.struct.intro]
@@ -44,11 +41,13 @@ The following are examples of struct expressions:
4441
# struct TuplePoint(f64, f64);
4542
# mod game { pub struct User<'a> { pub name: &'a str, pub age: u32, pub score: usize } }
4643
# struct Cookie; fn some_fn<T>(t: T) {}
44+
# enum Enum { Variant {} }
4745
Point {x: 10.0, y: 20.0};
4846
NothingInMe {};
4947
TuplePoint(10.0, 20.0);
5048
TuplePoint { 0: 10.0, 1: 20.0 }; // Results in the same value as the above line
5149
let u = game::User {name: "Joe", age: 35, score: 100_000};
50+
Enum::Variant {};
5251
some_fn::<Cookie>(Cookie);
5352
```
5453

@@ -119,29 +118,66 @@ Point3d { x, y: y_value, z };
119118
r[expr.struct.tuple]
120119
## Tuple struct expression
121120

122-
A struct expression with fields enclosed in parentheses constructs a tuple struct.
123-
Though it is listed here as a specific expression for completeness, it is equivalent to a [call expression] to the tuple struct's constructor. For example:
121+
A struct expression with fields enclosed in parentheses constructs a tuple struct or a tuple variant of an enum.
122+
Though it is listed here as a specific expression for completeness, it is equivalent to a [call expression] to the tuple struct's (enum tuple variant's) constructor. For example:
124123

125124
```rust
126125
struct Position(i32, i32, i32);
127126
Position(0, 0, 0); // Typical way of creating a tuple struct.
128127
let c = Position; // `c` is a function that takes 3 arguments.
129128
let pos = c(8, 6, 7); // Creates a `Position` value.
129+
130+
enum Version { Triple(i32, i32, i32) };
131+
Version::Triple(0, 0, 0);
132+
let f = Version::Triple;
133+
let ver = f(8, 6, 7);
130134
```
131135

136+
> [!NOTE]
137+
> While the grammar permits qualified paths, the last segment can't be a type alias:
138+
>
139+
> ```rust
140+
> trait Tr { type T; }
141+
> impl<T> Tr for T { type T = T; }
142+
>
143+
> struct Tuple();
144+
> enum Enum { Tuple() }
145+
>
146+
> // <Unit as Tr>::T(); // causes an error -- `::T` is a type, not a value
147+
> <Enum as Tr>::T::Tuple(); // OK
148+
> ```
149+
132150
r[expr.struct.unit]
133151
## Unit struct expression
134152
135-
A unit struct expression is just the path to a unit struct item.
136-
This refers to the unit struct's implicit constant of its value.
137-
The unit struct value can also be constructed with a fieldless struct expression. For example:
153+
A unit struct expression is just the path to a unit struct item or unit variant of an enum.
154+
This refers to the unit struct's (enum variant's) implicit constant of its value.
155+
The unit struct or a unit variant of an enum value can also be constructed with a fieldless struct expression. For example:
138156
139157
```rust
140158
struct Gamma;
141159
let a = Gamma; // Gamma unit value.
142160
let b = Gamma{}; // Exact same value as `a`.
161+
162+
enum ColorSpace { Oklch }
163+
let c = ColorSpace::Oklch;
164+
let d = ColorSpace::Oklch {};
143165
```
144166
167+
> [!NOTE]
168+
> While the grammar permits qualified paths, the last segment can't be a type alias:
169+
>
170+
> ```rust
171+
> trait Tr { type T; }
172+
> impl<T> Tr for T { type T = T; }
173+
>
174+
> struct Unit;
175+
> enum Enum { Unit }
176+
>
177+
> // <Unit as Tr>::T; // causes an error -- `::T` is a type, not a value
178+
> <Enum as Tr>::T::Unit; // OK
179+
> ```
180+
145181
[call expression]: call-expr.md
146182
[enum variant]: ../items/enumerations.md
147183
[if let]: if-expr.md#if-let-expressions

0 commit comments

Comments
 (0)