Skip to content

Commit 95a0133

Browse files
committed
Document irrefutable_let_patterns
1 parent dafc0ba commit 95a0133

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/expressions/if-expr.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ 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 an expression. If the value of the expression on the right
57+
hand side of the `=` matches the pattern, the corresponding block will
58+
execute, otherwise flow proceeds to the following `else` block if it exists.
59+
Like `if` expressions, `if let` expressions have a value determined by the
60+
block that is evaluated.
6161

6262
```rust
6363
let dish = ("Ham", "Eggs");
@@ -74,6 +74,12 @@ if let ("Bacon", b) = dish {
7474
if let ("Ham", b) = dish {
7575
println!("Ham is served with {}", b);
7676
}
77+
78+
// Irrefutable patterns are allowed primarily to make it easier for macros to
79+
// accept any kind of pattern.
80+
if let _ = 5 {
81+
println!("Irrefutable patterns are always true");
82+
}
7783
```
7884

7985
`if` and `if let` expressions can be intermixed:

src/expressions/loop-expr.md

+11-4
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,9 +71,9 @@ 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
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+
expression on the right hand side of the `=` matches the pattern, the loop
7777
body block executes then control returns to the pattern matching statement.
7878
Otherwise, the while expression completes.
7979

@@ -83,6 +83,13 @@ let mut x = vec![1, 2, 3];
8383
while let Some(y) = x.pop() {
8484
println!("y = {}", y);
8585
}
86+
87+
// Irrefutable patterns are allowed primarily to make it easier for macros to
88+
// accept any kind of pattern.
89+
while let _ = 5 {
90+
println!("Irrefutable patterns are always true");
91+
break;
92+
}
8693
```
8794

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

0 commit comments

Comments
 (0)