Skip to content

Commit 7c94f29

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 7c94f29

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/attributes/type_system.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ 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+
170+
```rust
170171
#[non_exhaustive]
171172
pub enum Example {
172173
First,
@@ -176,20 +177,21 @@ pub enum Example {
176177

177178
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:
178179

179-
```rust, ignore
180+
```rust
180181
#[non_exhaustive]
181-
pub enum Example {
182+
pub enum EnumWithNonExhaustiveVariants {
182183
First,
183184
#[non_exhaustive]
184185
Second
185186
}
186187
```
187188

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

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

0 commit comments

Comments
 (0)