Skip to content

Commit 8bc7420

Browse files
authored
Merge pull request #190 from kdnakt/if_let
Translate untranslated lines in if_let.md
2 parents 1ca6a0a + 379729f commit 8bc7420

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/flow_control/if_let.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@ fn main() {
8181
}
8282
```
8383

84+
<!--
8485
In the same way, `if let` can be used to match any enum value:
86+
-->
87+
同じように、`if let`を列挙型の値にマッチさせるのに利用できます。
8588

8689
```rust,editable
8790
// Our example enum
91+
// 列挙型の例
8892
enum Foo {
8993
Bar,
9094
Baz,
@@ -93,49 +97,68 @@ enum Foo {
9397
9498
fn main() {
9599
// Create example variables
100+
// 変数の例を作成する
96101
let a = Foo::Bar;
97102
let b = Foo::Baz;
98103
let c = Foo::Qux(100);
99104
100105
// Variable a matches Foo::Bar
106+
// 変数aはFoo::Barにマッチする
101107
if let Foo::Bar = a {
102108
println!("a is foobar");
103109
}
104110
105111
// Variable b does not match Foo::Bar
106112
// So this will print nothing
113+
// 変数bはFoo::Barにマッチしないので、これは何も出力しない
107114
if let Foo::Bar = b {
108115
println!("b is foobar");
109116
}
110117
111118
// Variable c matches Foo::Qux which has a value
112119
// Similar to Some() in the previous example
120+
// 変数cはFoo::Quxにマッチし、値を持つ
121+
// 以前のSome()の例と同様
113122
if let Foo::Qux(value) = c {
114123
println!("c is {}", value);
115124
}
116125
117126
// Binding also works with `if let`
127+
// `if let`でも束縛は動作する
118128
if let Foo::Qux(value @ 100) = c {
119129
println!("c is one hundred");
120130
}
121131
}
122132
```
123133

134+
<!--
124135
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.
136+
-->
137+
`if let`を利用する別の利点は、パラメータ化されていない列挙型の値をマッチさせられることです。
138+
これは、列挙型が`PartialEq`を実装もderiveもしていない場合でも同様です。
139+
`PartialEq`がない場合には、`if Foo::Bar == a`はコンパイルできません。
140+
列挙型のインスタンスは比較できませんが、`if let`を使えば動作します。
125141

142+
<!--
126143
Would you like a challenge? Fix the following example to use `if let`:
144+
-->
145+
次の例を`if let`を利用して修正するのにチャレンジしてみましょう。
127146

128147
```rust,editable,ignore,mdbook-runnable
129148
// This enum purposely neither implements nor derives PartialEq.
130149
// That is why comparing Foo::Bar == a fails below.
150+
// この列挙型はわざとPartialEqを実装もderiveもしていない
151+
// ゆえに以下でFoo::Bar == aの比較が失敗する
131152
enum Foo {Bar}
132153
133154
fn main() {
134155
let a = Foo::Bar;
135156
136157
// Variable a matches Foo::Bar
158+
// 変数aはFoo::Barにマッチする
137159
if Foo::Bar == a {
138160
// ^-- this causes a compile-time error. Use `if let` instead.
161+
// ^-- これはコンパイル時エラー。代わりに`if let`を使う。
139162
println!("a is foobar");
140163
}
141164
}

0 commit comments

Comments
 (0)