Understanding and Fixing Rust Error E0532: Pattern Arm Mismatch
Overview of Rust Error E0532
Rust Error E0532 occurs when the pattern arm in a match expression does not match the expected kind. This is usually caused by a mismatch between the data types used in the pattern arm and the expression being matched.
Erroneous Code Example
#![allow(unused)]
fn main() {
enum State {
Succeeded,
Failed(String),
}
fn print_on_failure(state: &State) {
match *state {
State::Failed => println!("Failed"),
_ => ()
}
}
}
In the above example, the following line causes the E0532 error:
State::Failed => println!("Failed"),
This is because the 'Failed' variant of the 'State' enum is a tuple variant with a String field. However, the pattern arm is written as if it were a unit variant or constant.
Solution to Rust Error E0532
To fix Rust Error E0532, ensure the match arm kind matches the expression being matched. In this case, the 'Failed' variant needs to include the necessary tuple field.
Fixed Code Example
#![allow(unused)]
fn main() {
enum State {
Succeeded,
Failed(String),
}
fn print_on_failure(state: &State) {
match *state {
State::Failed(ref msg) => println!("Failed with {}", msg),
_ => ()
}
}
}
In the fixed code example, the pattern arm correctly recognizes the 'Failed' variant as a tuple with a 'msg' field. The error message will now include the message from the 'Failed' variant.