Skip to content

Commit a2e277e

Browse files
committed
Add enum discriminats to the reference.
Fixes #15755
1 parent 868669f commit a2e277e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/doc/reference.md

+21
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,27 @@ a = Animal::Cat { name: "Spotty".to_string(), weight: 2.7 };
14131413
In this example, `Cat` is a _struct-like enum variant_,
14141414
whereas `Dog` is simply called an enum variant.
14151415

1416+
Enums have a discriminant. You can assign them explicitly:
1417+
1418+
```
1419+
enum Foo {
1420+
Bar = 123,
1421+
}
1422+
```
1423+
1424+
If a discriminant isn't assigned, they start at zero, and add one for each
1425+
variant, in order.
1426+
1427+
You can cast an enum to get this value:
1428+
1429+
```
1430+
# enum Foo { Bar = 123 }
1431+
let x = Foo::Bar as u32; // x is now 123u32
1432+
```
1433+
1434+
This only works as long as none of the variants have data attached. If
1435+
it were `Bar(i32)`, this is disallowed.
1436+
14161437
### Constant items
14171438

14181439
```{.ebnf .gram}

0 commit comments

Comments
 (0)