Skip to content

Commit db4f1f2

Browse files
authored
Merge pull request #515 from ehuss/irrefutable_let_patterns
Document irrefutable_let_patterns
2 parents 145e008 + 38787e4 commit db4f1f2

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/expressions/if-expr.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ assert_eq!(y, "Bigger");
5353
5454
An `if let` expression is semantically similar to an `if` expression but in
5555
place of a condition expression it expects the keyword `let` followed by a
56-
refutable pattern, an `=` and an expression. If the value of the expression on
57-
the right hand side of the `=` matches the pattern, the corresponding block
58-
will execute, otherwise flow proceeds to the following `else` block if it
59-
exists. Like `if` expressions, `if let` expressions have a value determined by
60-
the block that is evaluated.
56+
pattern, an `=` and a [scrutinee] expression. If the value of the scrutinee
57+
matches the pattern, the corresponding block will execute. Otherwise, flow
58+
proceeds to the following `else` block if it exists. Like `if` expressions,
59+
`if let` expressions have a value determined by the block that is evaluated.
6160

6261
```rust
6362
let dish = ("Ham", "Eggs");
@@ -74,6 +73,10 @@ if let ("Bacon", b) = dish {
7473
if let ("Ham", b) = dish {
7574
println!("Ham is served with {}", b);
7675
}
76+
77+
if let _ = 5 {
78+
println!("Irrefutable patterns are always true");
79+
}
7780
```
7881

7982
`if` and `if let` expressions can be intermixed:
@@ -136,3 +139,4 @@ if let PAT = ( EXPR || EXPR ) { .. }
136139
[_Pattern_]: patterns.html
137140
[_LazyBooleanOperatorExpression_]: expressions/operator-expr.html#lazy-boolean-operators
138141
[_eRFCIfLetChain_]: https://github.com/rust-lang/rfcs/blob/master/text/2497-if-let-chains.md#rollout-plan-and-transitioning-to-rust-2018
142+
[scrutinee]: glossary.html#scrutinee

src/expressions/loop-expr.md

+11-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Rust supports four loop expressions:
1919

2020
* A [`loop` expression](#infinite-loops) denotes an infinite loop.
2121
* A [`while` expression](#predicate-loops) loops until a predicate is false.
22-
* A [`while let` expression](#predicate-pattern-loops) tests a refutable pattern.
22+
* A [`while let` expression](#predicate-pattern-loops) tests a pattern.
2323
* A [`for` expression](#iterator-loops) extracts values from an iterator,
2424
looping until the iterator is empty.
2525

@@ -71,18 +71,23 @@ while i < 10 {
7171
> [_BlockExpression_]
7272
7373
A `while let` loop is semantically similar to a `while` loop but in place of a
74-
condition expression it expects the keyword `let` followed by a refutable
75-
pattern, an `=`, a [scrutinee] expression and a block expression. If the value of
76-
the expression on the right hand side of the `=` matches the pattern, the loop
77-
body block executes then control returns to the pattern matching statement.
78-
Otherwise, the while expression completes.
74+
condition expression it expects the keyword `let` followed by a pattern, an
75+
`=`, a [scrutinee] expression and a block expression. If the value of the
76+
scrutinee matches the pattern, the loop body block executes then control
77+
returns to the pattern matching statement. Otherwise, the while expression
78+
completes.
7979

8080
```rust
8181
let mut x = vec![1, 2, 3];
8282

8383
while let Some(y) = x.pop() {
8484
println!("y = {}", y);
8585
}
86+
87+
while let _ = 5 {
88+
println!("Irrefutable patterns are always true");
89+
break;
90+
}
8691
```
8792

8893
A `while let` loop is equivalent to a `loop` expression containing a `match`

0 commit comments

Comments
 (0)