Skip to content

Commit 335767e

Browse files
committed
add examples of constructing enum variants
1 parent a0f6fae commit 335767e

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/expressions/struct-expr.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ The following are examples of struct expressions:
4444
# struct TuplePoint(f64, f64);
4545
# mod game { pub struct User<'a> { pub name: &'a str, pub age: u32, pub score: usize } }
4646
# struct Cookie; fn some_fn<T>(t: T) {}
47+
# enum Enum { Variant {} }
4748
Point {x: 10.0, y: 20.0};
4849
NothingInMe {};
4950
TuplePoint(10.0, 20.0);
5051
TuplePoint { 0: 10.0, 1: 20.0 }; // Results in the same value as the above line
5152
let u = game::User {name: "Joe", age: 35, score: 100_000};
53+
Enum::Variant {};
5254
some_fn::<Cookie>(Cookie);
5355
```
5456

@@ -119,14 +121,19 @@ Point3d { x, y: y_value, z };
119121
r[expr.struct.tuple]
120122
## Tuple struct expression
121123

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:
124+
A struct expression with fields enclosed in parentheses constructs a tuple struct or a tuple variant of an enum.
125+
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:
124126

125127
```rust
126128
struct Position(i32, i32, i32);
127129
Position(0, 0, 0); // Typical way of creating a tuple struct.
128130
let c = Position; // `c` is a function that takes 3 arguments.
129131
let pos = c(8, 6, 7); // Creates a `Position` value.
132+
133+
enum Version { Triple(i32, i32, i32) };
134+
Version::Triple(0, 0, 0);
135+
let f = Version::Triple;
136+
let ver = f(8, 6, 7);
130137
```
131138

132139
> [!NOTE]
@@ -146,14 +153,18 @@ let pos = c(8, 6, 7); // Creates a `Position` value.
146153
r[expr.struct.unit]
147154
## Unit struct expression
148155
149-
A unit struct expression is just the path to a unit struct item.
150-
This refers to the unit struct's implicit constant of its value.
151-
The unit struct value can also be constructed with a fieldless struct expression. For example:
156+
A unit struct expression is just the path to a unit struct item or unit variant of an enum.
157+
This refers to the unit struct's (enum variant's) implicit constant of its value.
158+
The unit struct or a unit variant of an enum value can also be constructed with a fieldless struct expression. For example:
152159
153160
```rust
154161
struct Gamma;
155162
let a = Gamma; // Gamma unit value.
156163
let b = Gamma{}; // Exact same value as `a`.
164+
165+
enum ColorSpace { Oklch }
166+
let c = ColorSpace::Oklch;
167+
let d = ColorSpace::Oklch {}
157168
```
158169
159170
> [!NOTE]

0 commit comments

Comments
 (0)