You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/flow_control/if_let.md
+23Lines changed: 23 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -81,10 +81,14 @@ fn main() {
81
81
}
82
82
```
83
83
84
+
<!--
84
85
In the same way, `if let` can be used to match any enum value:
86
+
-->
87
+
同じように、`if let`を列挙型の値にマッチさせるのに利用できます。
85
88
86
89
```rust,editable
87
90
// Our example enum
91
+
// 列挙型の例
88
92
enum Foo {
89
93
Bar,
90
94
Baz,
@@ -93,49 +97,68 @@ enum Foo {
93
97
94
98
fn main() {
95
99
// Create example variables
100
+
// 変数の例を作成する
96
101
let a = Foo::Bar;
97
102
let b = Foo::Baz;
98
103
let c = Foo::Qux(100);
99
104
100
105
// Variable a matches Foo::Bar
106
+
// 変数aはFoo::Barにマッチする
101
107
if let Foo::Bar = a {
102
108
println!("a is foobar");
103
109
}
104
110
105
111
// Variable b does not match Foo::Bar
106
112
// So this will print nothing
113
+
// 変数bはFoo::Barにマッチしないので、これは何も出力しない
107
114
if let Foo::Bar = b {
108
115
println!("b is foobar");
109
116
}
110
117
111
118
// Variable c matches Foo::Qux which has a value
112
119
// Similar to Some() in the previous example
120
+
// 変数cはFoo::Quxにマッチし、値を持つ
121
+
// 以前のSome()の例と同様
113
122
if let Foo::Qux(value) = c {
114
123
println!("c is {}", value);
115
124
}
116
125
117
126
// Binding also works with `if let`
127
+
// `if let`でも束縛は動作する
118
128
if let Foo::Qux(value @ 100) = c {
119
129
println!("c is one hundred");
120
130
}
121
131
}
122
132
```
123
133
134
+
<!--
124
135
Another benefit is that `if let` allows us to match non-parameterized enum variants. This is true even in cases where the enum doesn't implement or derive `PartialEq`. In such cases `if Foo::Bar == a` would fail to compile, because instances of the enum cannot be equated, however `if let` will continue to work.
0 commit comments