File tree Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Expand file tree Collapse file tree 2 files changed +28
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments