From 7a6283028293694a4559e4fe75036f66621cc15e Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 26 Jan 2019 16:22:25 -0800 Subject: [PATCH 1/2] Document irrefutable_let_patterns --- src/expressions/if-expr.md | 16 +++++++++++----- src/expressions/loop-expr.md | 15 +++++++++++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/expressions/if-expr.md b/src/expressions/if-expr.md index 0395d7b00..a1f6e3411 100644 --- a/src/expressions/if-expr.md +++ b/src/expressions/if-expr.md @@ -53,11 +53,11 @@ assert_eq!(y, "Bigger"); An `if let` expression is semantically similar to an `if` expression but in place of a condition expression it expects the keyword `let` followed by a -refutable pattern, an `=` and an expression. If the value of the expression on -the right hand side of the `=` matches the pattern, the corresponding block -will execute, otherwise flow proceeds to the following `else` block if it -exists. Like `if` expressions, `if let` expressions have a value determined by -the block that is evaluated. +pattern, an `=` and an expression. If the value of the expression on the right +hand side of the `=` matches the pattern, the corresponding block will +execute, otherwise flow proceeds to the following `else` block if it exists. +Like `if` expressions, `if let` expressions have a value determined by the +block that is evaluated. ```rust let dish = ("Ham", "Eggs"); @@ -74,6 +74,12 @@ if let ("Bacon", b) = dish { if let ("Ham", b) = dish { println!("Ham is served with {}", b); } + +// Irrefutable patterns are allowed primarily to make it easier for macros to +// accept any kind of pattern. +if let _ = 5 { + println!("Irrefutable patterns are always true"); +} ``` `if` and `if let` expressions can be intermixed: diff --git a/src/expressions/loop-expr.md b/src/expressions/loop-expr.md index 51f514520..f8704f5f9 100644 --- a/src/expressions/loop-expr.md +++ b/src/expressions/loop-expr.md @@ -19,7 +19,7 @@ Rust supports four loop expressions: * A [`loop` expression](#infinite-loops) denotes an infinite loop. * A [`while` expression](#predicate-loops) loops until a predicate is false. -* A [`while let` expression](#predicate-pattern-loops) tests a refutable pattern. +* A [`while let` expression](#predicate-pattern-loops) tests a pattern. * A [`for` expression](#iterator-loops) extracts values from an iterator, looping until the iterator is empty. @@ -71,9 +71,9 @@ while i < 10 { > [_BlockExpression_] A `while let` loop is semantically similar to a `while` loop but in place of a -condition expression it expects the keyword `let` followed by a refutable -pattern, an `=`, a [scrutinee] expression and a block expression. If the value of -the expression on the right hand side of the `=` matches the pattern, the loop +condition expression it expects the keyword `let` followed by a pattern, an +`=`, a [scrutinee] expression and a block expression. If the value of the +expression on the right hand side of the `=` matches the pattern, the loop body block executes then control returns to the pattern matching statement. Otherwise, the while expression completes. @@ -83,6 +83,13 @@ let mut x = vec![1, 2, 3]; while let Some(y) = x.pop() { println!("y = {}", y); } + +// Irrefutable patterns are allowed primarily to make it easier for macros to +// accept any kind of pattern. +while let _ = 5 { + println!("Irrefutable patterns are always true"); + break; +} ``` A `while let` loop is equivalent to a `loop` expression containing a `match` From 38787e42a3a4902e5368ad1fe3e8d8d71493e89f Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 26 Jan 2019 17:18:54 -0800 Subject: [PATCH 2/2] Update for review. --- src/expressions/if-expr.md | 12 +++++------- src/expressions/loop-expr.md | 8 +++----- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/expressions/if-expr.md b/src/expressions/if-expr.md index a1f6e3411..25d3e5aec 100644 --- a/src/expressions/if-expr.md +++ b/src/expressions/if-expr.md @@ -53,11 +53,10 @@ assert_eq!(y, "Bigger"); An `if let` expression is semantically similar to an `if` expression but in place of a condition expression it expects the keyword `let` followed by a -pattern, an `=` and an expression. If the value of the expression on the right -hand side of the `=` matches the pattern, the corresponding block will -execute, otherwise flow proceeds to the following `else` block if it exists. -Like `if` expressions, `if let` expressions have a value determined by the -block that is evaluated. +pattern, an `=` and a [scrutinee] expression. If the value of the scrutinee +matches the pattern, the corresponding block will execute. Otherwise, flow +proceeds to the following `else` block if it exists. Like `if` expressions, +`if let` expressions have a value determined by the block that is evaluated. ```rust let dish = ("Ham", "Eggs"); @@ -75,8 +74,6 @@ if let ("Ham", b) = dish { println!("Ham is served with {}", b); } -// Irrefutable patterns are allowed primarily to make it easier for macros to -// accept any kind of pattern. if let _ = 5 { println!("Irrefutable patterns are always true"); } @@ -142,3 +139,4 @@ if let PAT = ( EXPR || EXPR ) { .. } [_Pattern_]: patterns.html [_LazyBooleanOperatorExpression_]: expressions/operator-expr.html#lazy-boolean-operators [_eRFCIfLetChain_]: https://github.com/rust-lang/rfcs/blob/master/text/2497-if-let-chains.md#rollout-plan-and-transitioning-to-rust-2018 +[scrutinee]: glossary.html#scrutinee diff --git a/src/expressions/loop-expr.md b/src/expressions/loop-expr.md index f8704f5f9..07eacc502 100644 --- a/src/expressions/loop-expr.md +++ b/src/expressions/loop-expr.md @@ -73,9 +73,9 @@ while i < 10 { A `while let` loop is semantically similar to a `while` loop but in place of a condition expression it expects the keyword `let` followed by a pattern, an `=`, a [scrutinee] expression and a block expression. If the value of the -expression on the right hand side of the `=` matches the pattern, the loop -body block executes then control returns to the pattern matching statement. -Otherwise, the while expression completes. +scrutinee matches the pattern, the loop body block executes then control +returns to the pattern matching statement. Otherwise, the while expression +completes. ```rust let mut x = vec![1, 2, 3]; @@ -84,8 +84,6 @@ while let Some(y) = x.pop() { println!("y = {}", y); } -// Irrefutable patterns are allowed primarily to make it easier for macros to -// accept any kind of pattern. while let _ = 5 { println!("Irrefutable patterns are always true"); break;