Skip to content

Commit ea1fe08

Browse files
committed
Fixup exhaustive enum examples
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.
1 parent efdf702 commit ea1fe08

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/attributes/type_system.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ match message {
166166
It's also not allowed to use numeric casts (`as`) on enums that contain any non-exhaustive variants.
167167

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

177177
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:
178178

179-
```rust, ignore
179+
```rust
180180
#[non_exhaustive]
181-
pub enum Example {
181+
pub enum EnumWithNonExhaustiveVariants {
182182
First,
183183
#[non_exhaustive]
184184
Second
185185
}
186186
```
187187

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

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

0 commit comments

Comments
 (0)