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() {
91
91
if let Foo::Qux(value) = c {
92
92
println!("c is {}", value);
93
93
}
94
+
95
+ // Binding also works with `if let`
96
+ if let Foo::Qux(value @ 100) = c {
97
+ println!("c is one hundred");
98
+ }
94
99
}
95
100
```
96
101
Original file line number Diff line number Diff line change @@ -26,7 +26,29 @@ fn main() {
26
26
}
27
27
```
28
28
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
+
29
49
### See also:
30
- [ functions]
50
+ [ ` functions ` ] [ functions ] , [ ` enums ` ] [ enums ] and [ ` Option ` ] [ option ]
31
51
32
52
[ 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