Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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