Hello,
I am trying to understand the rationale behind the why the commented code below does not work.
#[allow(dead_code)]
enum Message {
VariantA { x: i32, y: i32 }, // anonymoys c-like struct
VariantB ( i32, i32 ), // tuple struct
}
fn main() {
let m = Message::VariantA {x: 1, y: 2};
match m {
Message::VariantA {x, y} => println!("Variant A: {}, {}", x, y),
Message::VariantB (p, q) => println!("Variant B: {}, {}", p, q),
}
if let Message::VariantA {x, y} = m {
println!("Variant A (Second Time): {}, {}", x, y);
}
// the following code fails to compile
//match m {
// Message::VariantA {a, b} => println!("Variant A: {}, {}", a, b),
// Message::VariantB (p, q) => println!("Variant B: {}, {}", p, q),
//}
// the following code fails to compile
//if let Message::VariantA {a, b} = m {
// println!("Variant A (Second Time): {}, {}", a, b);
//}
}
Specifically speaking, it's allowed to destruct the tuple struct variant with variable names of my choice (this is easy to understand since there was no name defined for the members of the tuple struct). However, trying the same with c-like anonymous structs does not work.
Although not a blocker but the difference seems confusing and I want to understand the reasoning behind this.