Skip to content

Commit

Permalink
Fixup exhaustive enum examples
Browse files Browse the repository at this point in the history
Examples don't need to be ignored when they compile correctly.

Use the same enum name between the definition and the use. I think this
helps tie to the two examples together.

Don't use `default()` when default isn't derived in the example.
  • Loading branch information
ehuss committed Sep 10, 2024
1 parent efdf702 commit ea1fe08
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/attributes/type_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ match message {
It's also not allowed to use numeric casts (`as`) on enums that contain any non-exhaustive variants.

For example, the following enum can be cast because it doesn't contain any non-exhaustive variants:
```rust, ignore
```rust
#[non_exhaustive]
pub enum Example {
First,
Expand All @@ -176,20 +176,21 @@ pub enum Example {

However, if the enum contains even a single non-exhaustive variant, casting will result in an error. Consider this modified version of the same enum:

```rust, ignore
```rust
#[non_exhaustive]
pub enum Example {
pub enum EnumWithNonExhaustiveVariants {
First,
#[non_exhaustive]
Second
}
```

```rust, ignore
use othercrate::EnumWithNonExhaustiveEnumVariants;
<!-- ignore: needs multiple crates -->
```rust,ignore
use othercrate::EnumWithNonExhaustiveVariants;
// Error: cannot cast an enum with a non-exhaustive variant when it's defined in another crate
let _ = EnumWithNonExhaustiveEnumVariants::default() as u8;
let _ = EnumWithNonExhaustiveVariants::First as u8;
```

Non-exhaustive types are always considered inhabited in downstream crates.
Expand Down

0 comments on commit ea1fe08

Please sign in to comment.