Skip to content

Commit

Permalink
Merge pull request #1238 from pawroman/expand-bind-examples
Browse files Browse the repository at this point in the history
Add destructuring bind examples
  • Loading branch information
marioidival committed Aug 8, 2019
2 parents c46de59 + 47428d5 commit b74bd53
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/flow_control/if_let.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ fn main() {
if let Foo::Qux(value) = c {
println!("c is {}", value);
}
// Binding also works with `if let`
if let Foo::Qux(value @ 100) = c {
println!("c is one hundred");
}
}
```

Expand Down
24 changes: 23 additions & 1 deletion src/flow_control/match/binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,29 @@ fn main() {
}
```

You can also use binding to "destructure" `enum` variants, such as `Option`:

```rust,editable
fn some_number() -> Option<u32> {
Some(42)
}
fn main() {
match some_number() {
// Got `Some` variant, match if its value, bound to `n`,
// is equal to 42.
Some(n @ 42) => println!("The Answer: {}!", n),
// Match any other number.
Some(n) => println!("Not interesting... {}", n),
// Match anything else (`None` variant).
_ => (),
}
}
```

### See also:
[functions]
[`functions`][functions], [`enums`][enums] and [`Option`][option]

[functions]: ../../fn.md
[enums]: ../../custom_types/enum.md
[option]: ../../std/option.md

0 comments on commit b74bd53

Please sign in to comment.