-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Guide: match #15434
Conversation
|
||
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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....
There was a problem hiding this comment.
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.
I think this reasonable fine, although it doesn't mention (there's no particular need to mention them now if you have a plan):
Also, some things that could be mentioned at some point, but haven't got enough background:
|
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 |
|
||
`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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/becuase/because
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thank you.
@huonw on the three:
Can you think of a decent example of these two? I haven't used them enough. I do think they should be covered here.
|
@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 */ }
} |
@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. |
After thinking about it, this is good to go for now. I'm actually going to have to introduce @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: |
There was a problem hiding this comment.
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.
…=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.
No description provided.