Skip to content

Commit b74bd53

Browse files
authored
Merge pull request #1238 from pawroman/expand-bind-examples
Add destructuring bind examples
2 parents c46de59 + 47428d5 commit b74bd53

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/flow_control/if_let.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ fn main() {
9191
if let Foo::Qux(value) = c {
9292
println!("c is {}", value);
9393
}
94+
95+
// Binding also works with `if let`
96+
if let Foo::Qux(value @ 100) = c {
97+
println!("c is one hundred");
98+
}
9499
}
95100
```
96101

src/flow_control/match/binding.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,29 @@ fn main() {
2626
}
2727
```
2828

29+
You can also use binding to "destructure" `enum` variants, such as `Option`:
30+
31+
```rust,editable
32+
fn some_number() -> Option<u32> {
33+
Some(42)
34+
}
35+
36+
fn main() {
37+
match some_number() {
38+
// Got `Some` variant, match if its value, bound to `n`,
39+
// is equal to 42.
40+
Some(n @ 42) => println!("The Answer: {}!", n),
41+
// Match any other number.
42+
Some(n) => println!("Not interesting... {}", n),
43+
// Match anything else (`None` variant).
44+
_ => (),
45+
}
46+
}
47+
```
48+
2949
### See also:
30-
[functions]
50+
[`functions`][functions], [`enums`][enums] and [`Option`][option]
3151

3252
[functions]: ../../fn.md
53+
[enums]: ../../custom_types/enum.md
54+
[option]: ../../std/option.md

0 commit comments

Comments
 (0)