You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/attributes/type_system.md
+24-3Lines changed: 24 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -163,11 +163,32 @@ match message {
163
163
}
164
164
```
165
165
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
+
167
188
```rust, ignore
168
-
use othercrate::NonExhaustiveEnum;
189
+
use othercrate::NonExhaustiveEnumVariants;
169
190
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
0 commit comments