Skip to content

Guide: match #15434

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

Closed
wants to merge 1 commit into from
Closed

Guide: match #15434

wants to merge 1 commit into from

Conversation

steveklabnik
Copy link
Member

No description provided.


Finally, a comment about Windows. Rust considers Windows to be a first-class
platform upon release, but if we're honest, the Windows experience isn't as
integrated as the Linux/OS X experience is. We're working on it! If anything
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something missing here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoah whaoh. this whole section shouldn't be. My filesystem has been screwy lately....

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now right. Whoah.

@huonw
Copy link
Member

huonw commented Jul 5, 2014

I think this reasonable fine, although it doesn't mention (there's no particular need to mention them now if you have a plan):

  • name bindings (possibly incl. mut x)
  • nested patterns
  • pattern matching on tuples, structs or tuple structs (not hugely useful at the top level, but very useful as nested patterns inside enums etc.)

Also, some things that could be mentioned at some point, but haven't got enough background:

  • ref, ref mut patterns for binding a name as a reference into the matchee (these are what make pattern matching in Rust actually workable & performant)
  • box and & patterns for matching through pointers (well, only Box atm) and references respectively.

@steveklabnik
Copy link
Member Author

Yeah, the last two really needs pointers.

This isn't intended to be super exhaustive, so I left off name bindings on purpose. I haven't decided when to put in nested patterns, but it seems like a good plan for when I talk about Result. Matching on those other things seems good since I just talked about them.


`match` takes an expression, and then branches based on its value. Each 'arm' of
the branch is of the form `val => expression`. When the value matches, that arm's
expression will be evaluated. It's called `match` becuase of the term 'pattern
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/becuase/because

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thank you.

@steveklabnik
Copy link
Member Author

@huonw on the three:

  • pattern matching on tuples, structs or tuple structs: I think I want to cover this when I talk about Option, because that's when I've found it most useful.

Can you think of a decent example of these two? I haven't used them enough. I do think they should be covered here.

  • name bindings (possibly incl. mut x)
  • nested patterns

@sfackler
Copy link
Member

sfackler commented Jul 8, 2014

@steveklabnik here's on example of a place I've used a nested pattern:

match r.read(buf) {
    Ok(bytes) => { /* process buf */ }
    Err(IoError { kind: EndOfFile, .. }) => { /* bail out */ }
    Err(err) => { /* handle error */ }
}

@steveklabnik
Copy link
Member Author

@sfackler hmmm, interesting. That uses too many details I haven't talked about already, maybe that means it's a good candidate to put later in the guide.

@steveklabnik
Copy link
Member Author

After thinking about it, this is good to go for now. I'm actually going to have to introduce Option a bit earlier than I want, and so I have plans to add the other match stuff, but this should land first.

@brson r?


So what's the big advantage here? Well, there are a few. First of all, `match`
does 'exhaustiveness checking.' Do you see that last arm, the one with the
underscore (`_`)? If we remove that arm, Rust will give us an error:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to write the failing code here, so the readers can try it out and experience the error for themselves.

@bors bors closed this Jul 15, 2014
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 21, 2023
…=lnicola

minor : Deunwrap generate_derive

rust-lang#15398 subtask 1. Since the editing closure has arms, I did something *experimental* ( in this case just a clever term for bad code ) to bypass creating an `Option` but I am ready to change this.
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

Successfully merging this pull request may close these issues.

9 participants