diff --git a/src/expressions/if-expr.md b/src/expressions/if-expr.md index 0395d7b00..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 -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 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"); @@ -74,6 +73,10 @@ if let ("Bacon", b) = dish { if let ("Ham", b) = dish { println!("Ham is served with {}", b); } + +if let _ = 5 { + println!("Irrefutable patterns are always true"); +} ``` `if` and `if let` expressions can be intermixed: @@ -136,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 51f514520..07eacc502 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,11 +71,11 @@ 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 -body block executes then control returns to the pattern matching statement. -Otherwise, the while expression completes. +condition expression it expects the keyword `let` followed by a pattern, an +`=`, a [scrutinee] expression and a block expression. If the value of the +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]; @@ -83,6 +83,11 @@ let mut x = vec![1, 2, 3]; while let Some(y) = x.pop() { println!("y = {}", y); } + +while let _ = 5 { + println!("Irrefutable patterns are always true"); + break; +} ``` A `while let` loop is equivalent to a `loop` expression containing a `match`