Skip to content

Commit 4259530

Browse files
authored
Update type_system.md
1 parent 687faf9 commit 4259530

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/attributes/type_system.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,32 @@ match message {
163163
}
164164
```
165165

166-
It's also not allowed to cast non-exhaustive types from foreign crates.
166+
In Rust, casting non-exhaustive types from foreign crates is generally disallowed, except when dealing with enums that have no non-exhaustive variants.
167+
168+
For example, the following enum can be cast because it doesn't contain any non-exhaustive variants:
169+
```rust, ignore
170+
#[non_exhaustive]
171+
pub enum Example {
172+
First,
173+
Second
174+
}
175+
```
176+
177+
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:
178+
179+
```rust, ignore
180+
#[non_exhaustive]
181+
pub enum Example {
182+
First,
183+
#[non_exhaustive]
184+
Second
185+
}
186+
```
187+
167188
```rust, ignore
168-
use othercrate::NonExhaustiveEnum;
189+
use othercrate::NonExhaustiveEnumVariants;
169190
170-
// Cannot cast a non-exhaustive enum outside of its defining crate.
191+
// cannot cast an enum with a non-exhaustive variant when it's defined in another crate
171192
let _ = NonExhaustiveEnum::default() as u8;
172193
```
173194

0 commit comments

Comments
 (0)