Change:
Possibly link to the Rust Reference where this is explained in detail. Casting an enum to an int is common for C developers. When I searched the internet for guides on how to do this in Rust, they all essentially said it just works like . E.g. , . These guides don't explicitly say that this only works if all of the enum variants are unit-like. The compiler message in this situation did not help me to understand what had gone wrong, and googling for it and "enum" returned the same sort of results as mentioned above. No response No response <!-- TRIAGEBOTSTART --<!-- TRIAGEBOTASSIGNSTART --<!-- TRIAGEBOTASSIGNDATASTARTTRIAGEBOTASSIGNDATAEND --<!-- TRIAGEBOTASSIGNEND --<!-- TRIAGEBOTEND --
label D-newcomer-roadblock D-confusing
claim

Discussion:
); } } <del> let msg = "an `as` expression can only be used to convert between primitive  types or to coerce to a specific trait object"; </del> <ins> let (msg, note) = if let ty::Adt(adt, _) = self.expr_ty.kind() && adt.is_enum() && self.cast_ty.is_numeric() { ( "an `as` expression can be used to convert enum types to numeric  types only if the enum type is unit-only or field-less", Some( "see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information", ), ) } else { ( "an `as` expression can only be used to convert between primitive  types or to coerce to a specific trait object", None, ) }; </ins> if label { err.span_label(self.span, msg); } else { err.note(msg); } <ins> if let Some(note) = note { err.note(note); } </ins> } else { err.span_label(self.span, "invalid cast"); }

<ins> // Tests that `as` casts from enums to numeric types succeed // only if the enum type is "unit-only" or "fieldless" as // described here: https://doc.rust-lang.org/reference/items/enumerations.html#casting pub enum UnitOnly { Foo, Bar, Baz, } pub enum Fieldless { Tuple(), Struct{}, Unit, } pub enum NotUnitOnlyOrFieldless { Foo, Bar(u8), Baz } fn main() { let unit_only = UnitOnly::Foo; let _ = unit_only as isize; let _ = unit_only as i32; let _ = unit_only as usize; let _ = unit_only as u32; let fieldless = Fieldless::Struct{}; let _ = fieldless as isize; let _ = fieldless as i32; let _ = fieldless as usize; let _ = fieldless as u32; let not_unit_only_or_fieldless = NotUnitOnlyOrFieldless::Foo; let _ = not_unit_only_or_fieldless as isize; //~ ERROR non-primitive cast: `NotUnitOnlyOrFieldless` as `isize` let _ = not_unit_only_or_fieldless as i32; //~ ERROR non-primitive cast: `NotUnitOnlyOrFieldless` as `i32` let _ = not_unit_only_or_fieldless as usize; //~ ERROR non-primitive cast: `NotUnitOnlyOrFieldless` as `usize` let _ = not_unit_only_or_fieldless as u32; //~ ERROR non-primitive cast: `NotUnitOnlyOrFieldless` as `u32` } </ins>

<ins> error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `isize` --> $DIR/enum-to-numeric-cast.rs:42:13 | LL |     let _ = not_unit_only_or_fieldless as isize; |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less | = note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `i32` --> $DIR/enum-to-numeric-cast.rs:43:13 | LL |     let _ = not_unit_only_or_fieldless as i32; |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less | = note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `usize` --> $DIR/enum-to-numeric-cast.rs:44:13 | LL |     let _ = not_unit_only_or_fieldless as usize; |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less | = note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information error[E0605]: non-primitive cast: `NotUnitOnlyOrFieldless` as `u32` --> $DIR/enum-to-numeric-cast.rs:45:13 | LL |     let _ = not_unit_only_or_fieldless as u32; |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less | = note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0605`. </ins>

--> $DIR/issue-88621.rs:9:13 | LL |     let _ = Kind2::Foo() as u8; <del> |             ^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object </del> <ins> |             ^^^^^^^^^^^^^^^^^^ an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less | = note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information </ins> error: aborting due to 1 previous error

LL |     let val = v as isize; |               ^^^^^^^^^^ help: consider using the `From` trait instead: `isize::from(v)` | <del> = note: an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object </del> <ins> = note: an `as` expression can be used to convert enum types to numeric types only if the enum type is unit-only or field-less = note: see https://doc.rust-lang.org/reference/items/enumerations.html#casting for more information </ins> error: aborting due to 1 previous error