Rust errors / Patterns and matching
Rust compiler error · checked with rustc 1.99.0
Rust error E0532: expected unit struct, unit variant or constant, found tuple variant `State::Failed`
What it meansPattern arm did not match expected kind.
What rustc prints
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `State::Failed`
--> main.rs:10:9
|
3 | Failed(String),
| -------------- `State::Failed` defined here
...
10 | State::Failed => println!("Failed"),
| ^^^^^^^^^^^^^ help: use the tuple variant pattern syntax instead: `State::Failed(_)`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
Pattern arm did not match expected kind.
Erroneous code example:
enum State {
Succeeded,
Failed(String),
}
fn print_on_failure(state: &State) {
match *state {
// error: expected unit struct, unit variant or constant, found tuple
// variant `State::Failed`
State::Failed => println!("Failed"),
_ => ()
}
}
To fix this error, ensure the match arm kind is the same as the expression matched.
Fixed example:
enum State {
Succeeded,
Failed(String),
}
fn print_on_failure(state: &State) {
match *state {
State::Failed(ref msg) => println!("Failed with {}", msg),
_ => ()
}
}From rustc --explain E0532 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0004non-exhaustive patterns
- E0528pattern requires at least 3 elements but array has 2
- E0530match bindings cannot shadow tuple variants
- E0533expected unit struct, unit variant or constant, found associated function `Tortoise::turtle`
- E0638This error indicates that the struct, enum or enum variant must be matched non-exhaustively as it has been marked as `non_exhaustive`
Up next
Often hit nextE0004: non-exhaustive patternsA related error from the same family.Read it next →Did this answer your question?