Skip to content

Struct pattern matching #137

Open
Open
@jonathanpallant

Description

@jonathanpallant

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 */ }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions