diff --git a/src/flow_control/if_let.md b/src/flow_control/if_let.md index bcdebcedbd..720428d4f1 100644 --- a/src/flow_control/if_let.md +++ b/src/flow_control/if_let.md @@ -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"); + } } ``` diff --git a/src/flow_control/match/binding.md b/src/flow_control/match/binding.md index d3e5d46fdb..e1aa232169 100644 --- a/src/flow_control/match/binding.md +++ b/src/flow_control/match/binding.md @@ -26,7 +26,29 @@ fn main() { } ``` +You can also use binding to "destructure" `enum` variants, such as `Option`: + +```rust,editable +fn some_number() -> Option { + 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