Why this fires¶
A variant's explicit discriminant expression (= expr) either didn't evaluate to an integer at compile time, or couldn't be evaluated as a compile-time constant at all.
enum E: u8 { X = "zero" }
// ^^^^^^ error[TE118]: discriminant for `E::X` must be an integer constantfn compute() -> int { return 1 }
enum F: u8 { Y = compute() }
// ^^^^^^^^^ error[TE118]: discriminant for `F::Y` is not a compile-time constantFix it¶
Use a literal integer, or an expression the comptime evaluator can fold (arithmetic on literals, references to consts):
enum E: u8 { X = 0 } // ok
const BASE = 10
enum F: u8 { Y = BASE + 1 } // ok — comptime-foldableSee also¶
TE114— numeric discriminants require a non-generic, all-unit enum.TE115— discriminant value doesn't fit the backing type.- /docs/data-structures#enums — numeric enum discriminants.