186
186
187
187
188
188
< h1 class ="title "> if</ h1 >
189
- < p > Rust’s take on < code > if</ code > is not particularly complex, but it’s much more like the
190
- < code > if</ code > you’ll find in a dynamically typed language than in a more traditional
191
- systems language. So let’s talk about it, to make sure you grasp the nuances.</ p >
189
+ <!-- % if -->
192
190
193
- < p > < code > if</ code > is a specific form of a more general concept, the ‘branch’. The name comes
194
- from a branch in a tree: a decision point, where depending on a choice,
195
- multiple paths can be taken.</ p >
191
+ <!-- Rust’s take on `if` is not particularly complex, but it’s much more like the -->
196
192
197
- < p > In the case of < code > if</ code > , there is one choice that leads down two paths:</ p >
193
+ <!-- `if` you’ll find in a dynamically typed language than in a more traditional -->
194
+
195
+ <!-- systems language. So let’s talk about it, to make sure you grasp the nuances. -->
196
+
197
+ < p > Rustにおける < code > if</ code > の扱いはさほど複雑ではありませんが、伝統的なシステムプログラミング言語のそれに比べて、
198
+ 動的型付け言語でみられる < code > if</ code > にずっと近いものになっています。そのニュアンスをしっかり理解できるよう、
199
+ さっそく説明していきましょう。</ p >
200
+
201
+ <!-- `if` is a specific form of a more general concept, the ‘branch’. The name comes -->
202
+
203
+ <!-- from a branch in a tree: a decision point, where depending on a choice, -->
204
+
205
+ <!-- multiple paths can be taken. -->
206
+
207
+ < p > < code > if</ code > は一般化されたコンセプト、「分岐(branch)」の特別な形式です。この名前は木の枝(branch)を由来とし:
208
+ 取りうる複数のパスから、選択の決定を行うポイントを表します。</ p >
209
+
210
+ <!-- In the case of `if`, there is one choice that leads down two paths: -->
211
+
212
+ < p > < code > if</ code > の場合は、続く2つのパスから1つを選択します。</ p >
198
213
< span class ='rusttest '> fn main() {
199
214
let x = 5;
200
215
201
216
if x == 5 {
202
- println!("x is five!");
217
+ // println!("x is five!");
218
+ println!("x は 5 です!");
203
219
}
204
220
}</ span > < pre class ='rust rust-example-rendered '>
205
221
< span class ='kw '> let</ span > < span class ='ident '> x</ span > < span class ='op '> =</ span > < span class ='number '> 5</ span > ;
206
222
207
223
< span class ='kw '> if</ span > < span class ='ident '> x</ span > < span class ='op '> ==</ span > < span class ='number '> 5</ span > {
208
- < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x is five !"</ span > );
224
+ < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x は 5 です !"</ span > );
209
225
}</ pre >
210
226
211
- < p > If we changed the value of < code > x </ code > to something else, this line would not print.
212
- More specifically, if the expression after the < code > if </ code > evaluates to < code > true </ code > , then
213
- the block is executed. If it’s < code > false </ code > , then it is not. </ p >
227
+ <!-- If we changed the value of `x` to something else, this line would not print. -->
228
+
229
+ <!-- More specifically, if the expression after the `if` evaluates to `true` , then -- >
214
230
215
- < p > If you want something to happen in the < code > false</ code > case, use an < code > else</ code > :</ p >
231
+ <!-- the block is executed. If it’s `false`, then it is not. -->
232
+
233
+ < p > 仮に < code > x</ code > を別の値へと変更すると、この行は表示されません。より正確に言うなら、
234
+ < code > if</ code > のあとにくる式が < code > true</ code > に評価された場合に、ブロックが実行されます。
235
+ < code > false</ code > の場合、ブロックは実行されません。</ p >
236
+
237
+ <!-- If you want something to happen in the `false` case, use an `else`: -->
238
+
239
+ < p > < code > false</ code > の場合にも何かをしたいなら、 < code > else</ code > を使います:</ p >
216
240
< span class ='rusttest '> fn main() {
217
241
let x = 5;
218
242
219
243
if x == 5 {
220
- println!("x is five!");
244
+ // println!("x is five!");
245
+ println!("x は 5 です!");
221
246
} else {
222
- println!("x is not five :(");
247
+ // println!("x is not five :(");
248
+ println!("x は 5 ではありません :(");
223
249
}
224
250
}</ span > < pre class ='rust rust-example-rendered '>
225
251
< span class ='kw '> let</ span > < span class ='ident '> x</ span > < span class ='op '> =</ span > < span class ='number '> 5</ span > ;
226
252
227
253
< span class ='kw '> if</ span > < span class ='ident '> x</ span > < span class ='op '> ==</ span > < span class ='number '> 5</ span > {
228
- < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x is five !"</ span > );
254
+ < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x は 5 です !"</ span > );
229
255
} < span class ='kw '> else</ span > {
230
- < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x is not five :("</ span > );
256
+ < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x は 5 ではありません :("</ span > );
231
257
}</ pre >
232
258
233
- < p > If there is more than one case, use an < code > else if</ code > :</ p >
259
+ <!-- If there is more than one case, use an `else if`: -->
260
+
261
+ < p > 場合分けが複数あるときは、 < code > else if</ code > を使います:</ p >
234
262
< span class ='rusttest '> fn main() {
235
263
let x = 5;
236
264
237
265
if x == 5 {
238
- println!("x is five!");
266
+ // println!("x is five!");
267
+ println!("x は 5 です!");
239
268
} else if x == 6 {
240
- println!("x is six!");
269
+ // println!("x is six!");
270
+ println!("x は 6 です!");
241
271
} else {
242
- println!("x is not five or six :(");
272
+ // println!("x is not five or six :(");
273
+ println!("x は 5 でも 6 でもありません :(");
243
274
}
244
275
}</ span > < pre class ='rust rust-example-rendered '>
245
276
< span class ='kw '> let</ span > < span class ='ident '> x</ span > < span class ='op '> =</ span > < span class ='number '> 5</ span > ;
246
277
247
278
< span class ='kw '> if</ span > < span class ='ident '> x</ span > < span class ='op '> ==</ span > < span class ='number '> 5</ span > {
248
- < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x is five !"</ span > );
279
+ < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x は 5 です !"</ span > );
249
280
} < span class ='kw '> else</ span > < span class ='kw '> if</ span > < span class ='ident '> x</ span > < span class ='op '> ==</ span > < span class ='number '> 6</ span > {
250
- < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x is six !"</ span > );
281
+ < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x は 6 です !"</ span > );
251
282
} < span class ='kw '> else</ span > {
252
- < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x is not five or six :("</ span > );
283
+ < span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x は 5 でも 6 でもありません :("</ span > );
253
284
}</ pre >
254
285
255
- < p > This is all pretty standard. However, you can also do this:</ p >
286
+ <!-- This is all pretty standard. However, you can also do this: -->
287
+
288
+ < p > 全くもって普通ですね。しかし、次のような使い方もできるのです:</ p >
256
289
< span class ='rusttest '> fn main() {
257
290
let x = 5;
258
291
@@ -270,7 +303,9 @@ <h1 class="title">if</h1>
270
303
< span class ='number '> 15</ span >
271
304
}; < span class ='comment '> // y: i32</ span > </ pre >
272
305
273
- < p > Which we can (and probably should) write like this:</ p >
306
+ <!-- Which we can (and probably should) write like this: -->
307
+
308
+ < p > 次のように書くこともできます(そして、大抵はこう書くべきです):</ p >
274
309
< span class ='rusttest '> fn main() {
275
310
let x = 5;
276
311
@@ -280,9 +315,14 @@ <h1 class="title">if</h1>
280
315
281
316
< span class ='kw '> let</ span > < span class ='ident '> y</ span > < span class ='op '> =</ span > < span class ='kw '> if</ span > < span class ='ident '> x</ span > < span class ='op '> ==</ span > < span class ='number '> 5</ span > { < span class ='number '> 10</ span > } < span class ='kw '> else</ span > { < span class ='number '> 15</ span > }; < span class ='comment '> // y: i32</ span > </ pre >
282
317
283
- < p > This works because < code > if</ code > is an expression. The value of the expression is the
284
- value of the last expression in whichever branch was chosen. An < code > if</ code > without an
285
- < code > else</ code > always results in < code > ()</ code > as the value.</ p >
318
+ <!-- This works because `if` is an expression. The value of the expression is the -->
319
+
320
+ <!-- value of the last expression in whichever branch was chosen. An `if` without an -->
321
+
322
+ <!-- `else` always results in `()` as the value. -->
323
+
324
+ < p > これが出来るのは < code > if</ code > が式であるためです。その式の値は、選択された分岐中の最後の式の値となります。
325
+ < code > else</ code > のない < code > if</ code > では、その値は常に < code > ()</ code > となります。</ p >
286
326
287
327
< script type ="text/javascript ">
288
328
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments