Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct pattern matching #137

Open
jonathanpallant opened this issue Feb 28, 2024 · 1 comment
Open

Struct pattern matching #137

jonathanpallant opened this issue Feb 28, 2024 · 1 comment

Comments

@jonathanpallant
Copy link
Member

jonathanpallant commented Feb 28, 2024

Building on #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 */ }
}
@jonathanpallant
Copy link
Member Author

Although I note the book doesn't cover this syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant