Skip to content

Commit 40bb7fc

Browse files
committed
Rollup merge of rust-lang#25291 - johannhof:let-expressions-example, r=steveklabnik
Maybe it's me, but I really needed an example to understand if let and refutable statements. Playpen: http://is.gd/mjX3Gf Let me know if the variable names are too, uh, culinary.
2 parents bd764db + 6a19046 commit 40bb7fc

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/doc/reference.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3072,6 +3072,20 @@ of a condition expression it expects a refutable let statement. If the value of
30723072
expression on the right hand side of the let statement matches the pattern, the corresponding
30733073
block will execute, otherwise flow proceeds to the first `else` block that follows.
30743074

3075+
```
3076+
let dish = ("Ham", "Eggs");
3077+
3078+
// this body will be skipped because the pattern is refuted
3079+
if let ("Bacon", b) = dish {
3080+
println!("Bacon is served with {}", b);
3081+
}
3082+
3083+
// this body will execute
3084+
if let ("Ham", b) = dish {
3085+
println!("Ham is served with {}", b);
3086+
}
3087+
```
3088+
30753089
### While let loops
30763090

30773091
A `while let` loop is semantically identical to a `while` loop but in place of a

0 commit comments

Comments
 (0)