rust-training
rust-training copied to clipboard
Struct pattern matching
Building on https://github.com/ferrous-systems/rust-training/issues/103, we should perhaps explain pattern matching when the data contained in an enum variant is a struct.
enum Reason {
Finished,
Failed
}
enum Action {
Send,
Receive
}
enum Message {
Start,
Process(Action),
End { reason: Reason, timeout: u32 }
}
match message {
Message::Start => { /* pattern has no parameters */ }
Message::Process(Action::Send) => { /* two patterns in one */ }
Message::Process(a) => { /* grab the action as `a` */ }
Message::End { reason, timeout } => { /* must match field names */ }
}
Although I note the book doesn't cover this syntax.