Using pattern matching to unpack values into multiple variables can be useful, but using pattern matching to make decisions is where this functionality really shines, as in the following example:
let source2 = DemoStruct { id: 35, name: String::from("Another Thing"), probability: 0.42 };
let source3 = DemoStruct { id: 63, name: String::from("Super Thing"), probability: 0.99 };
if let DemoStruct { id: 63, name: y, probability: z } = source2 {
println!("When id is 63, name is {} and probability is {}", y, z);
}
if let DemoStruct { id: 63, name: y, probability: z } = source3 {
println!("When id is 63, name is {} and probability is {}", y, z);
}
Here, we've defined two more variables containing DemoStruct values, and then used pattern matching to pull them back apart and assign...