Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 605
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -67,7 +67,7 @@ while i < 10 { | ||
| > **<sup>Syntax</sup>**\ | ||
| > [_PredicatePatternLoopExpression_] :\ | ||
| > `while` `let` [_Pattern_] `=` [_Expression_]<sub>except struct expression</sub> | ||
| > `while` `let` [_MatchArmPatterns_] `=` [_Expression_]<sub>except struct expression</sub> | ||
| > [_BlockExpression_] | ||
| A `while let` loop is semantically similar to a `while` loop but in place of a | ||
| @@ -90,11 +90,11 @@ while let _ = 5 { | ||
| } | ||
| ``` | ||
| A `while let` loop is equivalent to a `loop` expression containing a `match` | ||
| expression as follows. | ||
| A `while let` loop is equivalent to a `loop` expression containing a [`match` | ||
| expression] as follows. | ||
| ```rust,ignore | ||
| 'label: while let PAT = EXPR { | ||
| 'label: while let PATS = EXPR { | ||
| /* loop body */ | ||
| } | ||
| ``` | ||
| @@ -104,12 +104,23 @@ is equivalent to | ||
| ```rust,ignore | ||
| 'label: loop { | ||
| match EXPR { | ||
| PAT => { /* loop body */ }, | ||
| PATS => { /* loop body */ }, | ||
| _ => break, | ||
| } | ||
| } | ||
| ``` | ||
| Multiple patterns may be specified with the `|` operator. This has the same semantics | ||
Centril marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| as with `|` in `match` expressions: | ||
| ```rust | ||
| let mut vals = vec![2, 3, 1, 2, 2]; | ||
| while let Some(v @ 1) | Some(v @ 2) = vals.pop() { | ||
| // Prints 2, 2, then 1 | ||
| println!("{}", v); | ||
| } | ||
| ``` | ||
| ## Iterator loops | ||
| > **<sup>Syntax</sup>**\ | ||
| @@ -272,12 +283,11 @@ and the `loop` must have a type compatible with each `break` expression. | ||
| expression `()`. | ||
| [IDENTIFIER]: identifiers.html | ||
| [temporary values]: expressions.html#temporary-lifetimes | ||
| [_Expression_]: expressions.html | ||
| [LIFETIME_OR_LABEL]: tokens.html#lifetimes-and-loop-labels | ||
| [_BlockExpression_]: expressions/block-expr.html | ||
| [_Expression_]: expressions.html | ||
| [_MatchArmPatterns_]: expressions/match-expr.html | ||
| [_Pattern_]: patterns.html | ||
| [LIFETIME_OR_LABEL]: tokens.html#lifetimes-and-loop-labels | ||
| [`match` expression]: expressions/match-expr.html | ||
| [scrutinee]: glossary.html#scrutinee | ||
| [temporary values]: expressions.html#temporary-lifetimes | ||
| 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.
Uh oh!There was an error while loading. Please reload this page. | ||
| ```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 | ||
Contributor 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 | ||
Uh oh!
There was an error while loading. Please reload this page.