-
Notifications
You must be signed in to change notification settings - Fork 537
Document if_while_or_patterns #516
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,8 @@ match x { | |
Variables bound within the pattern are scoped to the match guard and the arm's | ||
expression. The [binding mode] (move, copy, or reference) depends on the pattern. | ||
|
||
Multiple match patterns may be joined with the `|` operator: | ||
Multiple match patterns may be joined with the `|` operator. Each pattern will be | ||
tested in left-to-right sequence until a successful match is found. | ||
Centril marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```rust | ||
# let x = 9; | ||
|
@@ -73,19 +74,30 @@ let message = match x { | |
}; | ||
|
||
assert_eq!(message, "a few"); | ||
|
||
// Demonstration of pattern match order. | ||
struct S(i32, i32); | ||
|
||
match S(1, 2) { | ||
S(z @ 1, _) | S(_, z @ 2) => assert_eq!(z, 1), | ||
_ => panic!(), | ||
} | ||
``` | ||
|
||
Please notice that the `2..=9` is a [Range Pattern], not a [Range Expression] | ||
and, thus, only those types of ranges supported by range patterns can be used | ||
in match arms. | ||
> Note: The `2..=9` is a [Range Pattern], not a [Range Expression]. Thus, only | ||
> those types of ranges supported by range patterns can be used in match arms. | ||
|
||
Every binding in each `|` separated pattern must appear in all of the patterns | ||
in the arm. Every binding of the same name must have the same type, and have | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit vague since it doesn't say whether "same type" accounts for subtyping or not, but it's serviceable until we have a more formal semantics. |
||
the same binding mode. | ||
|
||
Match arms can accept _match guards_ to further refine the | ||
criteria for matching a case. Pattern guards appear after the pattern and | ||
consist of a bool-typed expression following the `if` keyword. A pattern guard | ||
may refer to the variables bound within the pattern they follow. | ||
|
||
When the pattern matches successfully, the pattern guard expression is executed. | ||
If the expression is truthy, the pattern is successfully matched against. | ||
If the expression evaluates to true, the pattern is successfully matched against. | ||
Otherwise, the next pattern, including other matches with the `|` operator in | ||
the same arm, is tested. | ||
|
||
|
@@ -104,15 +116,13 @@ let message = match maybe_digit { | |
> and side effects it has to execute multiple times. For example: | ||
> | ||
> ```rust | ||
> use std::cell::Cell; | ||
> fn main() { | ||
> let i : Cell<i32> = Cell::new(0); | ||
> match 1 { | ||
> 1 | _ if { i.set(i.get() + 1); false } => {} | ||
> _ => {} | ||
> } | ||
> assert_eq!(i.get(), 2); | ||
> # use std::cell::Cell; | ||
> let i : Cell<i32> = Cell::new(0); | ||
> match 1 { | ||
> 1 | _ if { i.set(i.get() + 1); false } => {} | ||
> _ => {} | ||
> } | ||
> assert_eq!(i.get(), 2); | ||
> ``` | ||
|
||
## Attributes on match arms | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.