Skip to content

Commit 340f3c9

Browse files
committed
Improve the if let example to have a binding pattern. Fixes #1401.
1 parent 6139cdb commit 340f3c9

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
fn main() {
22
// ANCHOR: here
3-
let some_u8_value = Some(0u8);
4-
match some_u8_value {
5-
Some(3) => println!("three"),
3+
let config_max = Some(3u8);
4+
match config_max {
5+
Some(max) => println!("The maximum is configured to be {}", max),
66
_ => (),
77
}
88
// ANCHOR_END: here
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
fn main() {
22
// ANCHOR: here
3-
let some_u8_value = Some(0u8);
4-
if let Some(3) = some_u8_value {
5-
println!("three");
3+
let config_max = Some(3u8);
4+
if let Some(max) = config_max {
5+
println!("The maximum is configured to be {}", max);
66
}
77
// ANCHOR_END: here
88
}

src/ch06-03-if-let.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22

33
The `if let` syntax lets you combine `if` and `let` into a less verbose way to
44
handle values that match one pattern while ignoring the rest. Consider the
5-
program in Listing 6-6 that matches on an `Option<u8>` value but only wants to
6-
execute code if the value is 3.
5+
program in Listing 6-6 that matches on an `Option<u8>` value in the `config_max`
6+
variable but only wants to execute code if the value is the `Some` variant.
77

88
```rust
99
{{#rustdoc_include ../listings/ch06-enums-and-pattern-matching/listing-06-06/src/main.rs:here}}
1010
```
1111

1212
<span class="caption">Listing 6-6: A `match` that only cares about executing
13-
code when the value is `Some(3)`</span>
13+
code when the value is `Some`</span>
1414

15-
We want to do something with the `Some(3)` match but do nothing with any other
16-
`Some<u8>` value or the `None` value. To satisfy the `match` expression, we
17-
have to add `_ => ()` after processing just one variant, which is a lot of
18-
boilerplate code to add.
15+
If the value is `Some`, we want to print out the value in the `Some` variant,
16+
which we do by binding the value to the variable `max` in the pattern.
17+
We don’t want to do anything with the `None` value. To satisfy the `match`
18+
expression, we have to add `_ => ()` after processing just one variant, which
19+
is annoying boilerplate code to add.
1920

2021
Instead, we could write this in a shorter way using `if let`. The following
2122
code behaves the same as the `match` in Listing 6-6:
@@ -26,7 +27,11 @@ code behaves the same as the `match` in Listing 6-6:
2627

2728
The syntax `if let` takes a pattern and an expression separated by an equal
2829
sign. It works the same way as a `match`, where the expression is given to the
29-
`match` and the pattern is its first arm.
30+
`match` and the pattern is its first arm. In this case, the pattern is
31+
`Some(max)`, and the `max` binds to the value inside the `Some`. We can then
32+
use `max` in the body of the `if let` block in the same way as we used `max` in
33+
the corresponding `match` arm. The code in the `if let` block isn’t run if the
34+
value doesn’t match the pattern.
3035

3136
Using `if let` means less typing, less indentation, and less boilerplate code.
3237
However, you lose the exhaustive checking that `match` enforces. Choosing

0 commit comments

Comments
 (0)