Rust errors / Attributes, macros and features
Rust compiler error · checked with rustc 1.99.0
Rust error E0731: transparent enum needs exactly one variant, but has 2
What it meansAn enum with the representation hint repr(transparent) had zero or more than one variants.
What rustc prints
error[E0731]: transparent enum needs exactly one variant, but has 2
--> main.rs:2:1
|
2 | enum Status { // error: transparent enum needs exactly one variant, but has 2
| ^^^^^^^^^^^ needs exactly one variant, but has 2
3 | Errno(u32),
| ----- variant here
4 | Ok,
| -- too many variants in `Status`Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
The compiler team's explanation
An enum with the representation hint repr(transparent) had zero or more than one variants.
Erroneous code example:
#[repr(transparent)]
enum Status { // error: transparent enum needs exactly one variant, but has 2
Errno(u32),
Ok,
}
Because transparent enums are represented exactly like one of their variants at run time, said variant must be uniquely determined. If there is no variant, or if there are multiple variants, it is not clear how the enum should be represented.
From rustc --explain E0731 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
Up next
Often hit nextE0658: is unstableA related error from the same family.Read it next →Did this answer your question?