Pular para o conteúdo
TE115 · Tipos · error

Discriminante não cabe no tipo de backing

O discriminante de uma variante está fora da faixa do tipo de backing declarado (`enum E: u8` limita a `0..=255`), então não sobreviveria ao ida-e-volta de `to_int()`/`from_int()`.

Why this fires

An enum variant's discriminant value doesn't fit inside the enum's declared backing type. enum E: u8 limits every discriminant to the 0..=255 range; a value outside that range can't round-trip through to_int()/from_int() on that backing type.

enum E: u8 { X = 300 }
//           ^^^^^^^ error[TE115]: discriminant 300 for `E::X` does not fit backing type `u8` (0..=255)

Fix it

1. Use a value that fits

enum E: u8 { X = 200 }          // ok — within 0..=255

2. Widen the backing type

enum E: u16 { X = 300 }         // ok — u16 covers 0..=65535

See also

  • TE114 — numeric discriminants require a non-generic, all-unit enum.
  • TE116 — duplicate discriminant value.
  • /docs/data-structures#enums — numeric enum discriminants and backing types.

Veja também

Buscar no Zolo

9 resultados

en