4
4
< meta charset ="utf-8 ">
5
5
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
6
< meta name ="generator " content ="rustdoc ">
7
- < title > Loops </ title >
7
+ < title > ループ </ title >
8
8
9
9
< link rel ="stylesheet " type ="text/css " href ="rustbook.css ">
10
10
185
185
< div id ='page '>
186
186
187
187
188
- < h1 class ="title "> Loops</ h1 >
189
- < p > Rust currently provides three approaches to performing some kind of iterative activity. They are: < code > loop</ code > , < code > while</ code > and < code > for</ code > . Each approach has its own set of uses.</ p >
188
+ < h1 class ="title "> ループ</ h1 >
189
+ <!-- % Loops -->
190
+
191
+ <!-- Rust currently provides three approaches to performing some kind of iterative activity. They are: `loop`, `while` and `for`. Each approach has its own set of uses. -->
192
+
193
+ < p > なんらかの繰り返しを伴う処理に対して、Rust言語は3種類のアプローチ: < code > loop</ code > , < code > while</ code > , < code > for</ code > を提供します。
194
+ 各アプローチにはそれぞれの使い道があります。</ p >
190
195
191
196
< h2 id ='loop ' class ='section-header '> < a href ='#loop '> loop</ a > </ h2 >
192
- < p > The infinite < code > loop</ code > is the simplest form of loop available in Rust. Using the keyword < code > loop</ code > , Rust provides a way to loop indefinitely until some terminating statement is reached. Rust's infinite < code > loop</ code > s look like this:</ p >
197
+ <!-- The infinite `loop` is the simplest form of loop available in Rust. Using the keyword `loop`, Rust provides a way to loop indefinitely until some terminating statement is reached. Rust's infinite `loop`s look like this: -->
198
+
199
+ < p > Rustで使えるループなかで最もシンプルな形式が、無限 < code > loop</ code > です。Rustのキーワード < code > loop</ code > によって、
200
+ 何らかの終了状態に到達するまでずっとループし続ける手段を提供します。Rustの無限 < code > loop</ code > はこのように:</ p >
193
201
< span class ='rusttest '> fn main() {
194
202
loop {
195
203
println!("Loop forever!");
@@ -200,7 +208,9 @@ <h2 id='loop' class='section-header'><a href='#loop'>loop</a></h2>
200
208
}</ pre >
201
209
202
210
< h2 id ='while ' class ='section-header '> < a href ='#while '> while</ a > </ h2 >
203
- < p > Rust also has a < code > while</ code > loop. It looks like this:</ p >
211
+ <!-- Rust also has a `while` loop. It looks like this: -->
212
+
213
+ < p > Rustには < code > while</ code > ループもあります。このように:</ p >
204
214
< span class ='rusttest '> fn main() {
205
215
let mut x = 5; // mut x: i32
206
216
let mut done = false; // mut done: bool
@@ -228,37 +238,60 @@ <h2 id='while' class='section-header'><a href='#while'>while</a></h2>
228
238
}
229
239
}</ pre >
230
240
231
- < p > < code > while</ code > loops are the correct choice when you’re not sure how many times
232
- you need to loop.</ p >
241
+ <!-- `while` loops are the correct choice when you’re not sure how many times -->
242
+
243
+ <!-- you need to loop. -->
244
+
245
+ < p > 何回ループする必要があるか明らかではない状況で、< code > while</ code > ループは正しい選択肢です。</ p >
246
+
247
+ <!-- If you need an infinite loop, you may be tempted to write this: -->
233
248
234
- < p > If you need an infinite loop, you may be tempted to write this :</ p >
249
+ < p > 無限ループの必要があるとき、次のように書きたくなるかもしれません :</ p >
235
250
< span class ='rusttest '> fn main() {
236
251
while true {
237
252
}</ span > < pre class ='rust rust-example-rendered '>
238
253
< span class ='kw '> while</ span > < span class ='boolval '> true</ span > {</ pre >
239
254
240
- < p > However, < code > loop</ code > is far better suited to handle this case:</ p >
255
+ <!-- However, `loop` is far better suited to handle this case: -->
256
+
257
+ < p > しかし、こういった場合には < code > loop</ code > の方がずっと適しています。</ p >
241
258
< span class ='rusttest '> fn main() {
242
259
loop {
243
260
}</ span > < pre class ='rust rust-example-rendered '>
244
261
< span class ='kw '> loop</ span > {</ pre >
245
262
246
- < p > Rust’s control-flow analysis treats this construct differently than a < code > while true</ code > , since we know that it will always loop. In general, the more information
247
- we can give to the compiler, the better it can do with safety and code
248
- generation, so you should always prefer < code > loop</ code > when you plan to loop
249
- infinitely.</ p >
263
+ <!-- Rust’s control-flow analysis treats this construct differently than a `while -->
264
+
265
+ <!-- true`, since we know that it will always loop. In general, the more information -->
266
+
267
+ <!-- we can give to the compiler, the better it can do with safety and code -->
268
+
269
+ <!-- generation, so you should always prefer `loop` when you plan to loop -->
270
+
271
+ <!-- infinitely. -->
272
+
273
+ < p > Rustの制御フロー解析では、必ずループすると知っていることから、これを < code > while true</ code > とは異なる構造として扱います。
274
+ 一般に、コンパイラへ与える情報量が多いほど、安全性が高くより良いコード生成につながるため、
275
+ 無限にループするつもりなら常に < code > loop</ code > を使うべきです。</ p >
250
276
251
277
< h2 id ='for ' class ='section-header '> < a href ='#for '> for</ a > </ h2 >
252
- < p > The < code > for</ code > loop is used to loop a particular number of times. Rust’s < code > for</ code > loops
253
- work a bit differently than in other systems languages, however. Rust’s < code > for</ code >
254
- loop doesn’t look like this “C-style” < code > for</ code > loop:</ p >
278
+ <!-- The `for` loop is used to loop a particular number of times. Rust’s `for` loops -->
279
+
280
+ <!-- work a bit differently than in other systems languages, however. Rust’s `for` -->
281
+
282
+ <!-- loop doesn’t look like this “C-style” `for` loop: -->
283
+
284
+ < p > 特定の回数だけループするときには < code > for</ code > ループを使います。しかし、Rustの < code > for</ code > ループは他のシステムプログラミング言語のそれとは少し異なる働きをします。
285
+ Rustの < code > for</ code > ループは、次のような「Cスタイル」 < code > for</ code > ループとは似ていません:</ p >
255
286
256
287
< pre > < code class ="language-c "> for (x = 0; x < 10; x++) {
257
288
printf( "%d\n", x );
258
289
}
259
290
</ code > </ pre >
260
291
261
- < p > Instead, it looks like this:</ p >
292
+ <!-- Instead, it looks like this: -->
293
+
294
+ < p > 代わりに、このように書きます:</ p >
262
295
< span class ='rusttest '> fn main() {
263
296
for x in 0..10 {
264
297
println!("{}", x); // x: i32
@@ -268,7 +301,9 @@ <h2 id='for' class='section-header'><a href='#for'>for</a></h2>
268
301
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "{}"</ span > , < span class ='ident '> x</ span > ); < span class ='comment '> // x: i32</ span >
269
302
}</ pre >
270
303
271
- < p > In slightly more abstract terms,</ p >
304
+ <!-- In slightly more abstract terms, -->
305
+
306
+ < p > もう少し抽象的な用語を使うと、</ p >
272
307
< span class ='rusttest '> fn main() {
273
308
for var in expression {
274
309
code
@@ -278,25 +313,52 @@ <h2 id='for' class='section-header'><a href='#for'>for</a></h2>
278
313
< span class ='ident '> code</ span >
279
314
}</ pre >
280
315
281
- < p > The expression is an item that can be converted into an < a href ="iterators.html "> iterator</ a > using
282
- < a href ="../std/iter/trait.IntoIterator.html "> < code > IntoIterator</ code > </ a > . The iterator gives back a series of elements. Each element is
283
- one iteration of the loop. That value is then bound to the name < code > var</ code > , which is
284
- valid for the loop body. Once the body is over, the next value is fetched from
285
- the iterator, and we loop another time. When there are no more values, the < code > for</ code >
286
- loop is over.</ p >
316
+ <!-- The expression is an item that can be converted into an [iterator] using -->
317
+
318
+ <!-- [`IntoIterator`]. The iterator gives back a series of elements. Each element is -->
319
+
320
+ <!-- one iteration of the loop. That value is then bound to the name `var`, which is -->
321
+
322
+ <!-- valid for the loop body. Once the body is over, the next value is fetched from -->
323
+
324
+ <!-- the iterator, and we loop another time. When there are no more values, the `for` -->
325
+
326
+ <!-- loop is over. -->
327
+
328
+ < p > 式(expression)は< a href ="../std/iter/trait.IntoIterator.html "> < code > IntoIterator</ code > </ a > を用いて< a href ="iterators.html "> イテレータ</ a > へと変換可能なアイテムです。
329
+ イテレータは要素の連なりを返します。それぞれの要素がループの1回の反復になります。
330
+ その値は名前 < code > var</ code > に束縛されて、ループ本体にて有効になります。いったんループ本体を抜けると、
331
+ 次の値がイテレータから取り出され、次のループ処理を行います。それ以上の値が存在しない時は、
332
+ < code > for</ code > ループは終了します。</ p >
333
+
334
+ <!-- In our example, `0..10` is an expression that takes a start and an end position, -->
335
+
336
+ <!-- and gives an iterator over those values. The upper bound is exclusive, though, -->
337
+
338
+ <!-- so our loop will print `0` through `9`, not `10`. -->
287
339
288
- < p > In our example, < code > 0..10</ code > is an expression that takes a start and an end position,
289
- and gives an iterator over those values. The upper bound is exclusive, though,
290
- so our loop will print < code > 0</ code > through < code > 9</ code > , not < code > 10</ code > .</ p >
340
+ < p > 例示では、< code > 0..10</ code > が開始位置と終了位置をとる式であり、同範囲の値を返すイテレータを与えます。
341
+ 上界はその値自身を含まないため、このループは < code > 0</ code > から < code > 9</ code > までを表示します。 < code > 10</ code > ではありません。</ p >
291
342
292
- < p > Rust does not have the “C-style” < code > for</ code > loop on purpose. Manually controlling
293
- each element of the loop is complicated and error prone, even for experienced C
294
- developers.</ p >
343
+ <!-- Rust does not have the “C-style” `for` loop on purpose. Manually controlling -->
295
344
296
- < h3 id ='enumerate ' class ='section-header '> < a href ='#enumerate '> Enumerate</ a > </ h3 >
297
- < p > When you need to keep track of how many times you already looped, you can use the < code > .enumerate()</ code > function.</ p >
345
+ <!-- each element of the loop is complicated and error prone, even for experienced C -->
298
346
299
- < h4 id ='on-ranges ' class ='section-header '> < a href ='#on-ranges '> On ranges:</ a > </ h4 > < span class ='rusttest '> fn main() {
347
+ <!-- developers. -->
348
+
349
+ < p > Rustでは意図的に「Cスタイル」 < code > for</ code > ループを持ちません。経験豊富なC開発者でさえ、
350
+ ループの各要素を手動制御することは複雑であり、また間違いを犯しやすいのです。</ p >
351
+
352
+ <!-- ### Enumerate -->
353
+
354
+ < h3 id ='列挙 ' class ='section-header '> < a href ='#列挙 '> 列挙</ a > </ h3 >
355
+ <!-- When you need to keep track of how many times you already looped, you can use the `.enumerate()` function. -->
356
+
357
+ < p > ループ中で何回目の繰り返しかを知る必要があるなら、 < code > .enumerate()</ code > 関数が使えます。</ p >
358
+
359
+ <!-- #### On ranges: -->
360
+
361
+ < h4 id ='レンジを対象に ' class ='section-header '> < a href ='#レンジを対象に '> レンジを対象に:</ a > </ h4 > < span class ='rusttest '> fn main() {
300
362
for (i,j) in (5..10).enumerate() {
301
363
println!("i = {} and j = {}", i, j);
302
364
}
@@ -305,7 +367,9 @@ <h4 id='on-ranges' class='section-header'><a href='#on-ranges'>On ranges:</a></h
305
367
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "i = {} and j = {}"</ span > , < span class ='ident '> i</ span > , < span class ='ident '> j</ span > );
306
368
}</ pre >
307
369
308
- < p > Outputs:</ p >
370
+ <!-- Outputs: -->
371
+
372
+ < p > 出力:</ p >
309
373
310
374
< pre > < code class ="language-text "> i = 0 and j = 5
311
375
i = 1 and j = 6
@@ -314,9 +378,13 @@ <h4 id='on-ranges' class='section-header'><a href='#on-ranges'>On ranges:</a></h
314
378
i = 4 and j = 9
315
379
</ code > </ pre >
316
380
317
- < p > Don' t forget to add the parentheses around the range.</ p >
381
+ <!-- Don' t forget to add the parentheses around the range. -- >
318
382
319
- < h4 id ='on-iterators ' class ='section-header '> < a href ='#on-iterators '> On iterators:</ a > </ h4 > < span class ='rusttest '> fn main() {
383
+ < p > レンジを括弧で囲うのを忘れないで下さい。</ p >
384
+
385
+ <!-- #### On iterators: -->
386
+
387
+ < h4 id ='イテレータを対象に ' class ='section-header '> < a href ='#イテレータを対象に '> イテレータを対象に:</ a > </ h4 > < span class ='rusttest '> fn main() {
320
388
let lines = "hello\nworld".lines();
321
389
for (linenumber, line) in lines.enumerate() {
322
390
println!("{}: {}", linenumber, line);
@@ -326,16 +394,22 @@ <h4 id='on-iterators' class='section-header'><a href='#on-iterators'>On iterator
326
394
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "{}: {}"</ span > , < span class ='ident '> linenumber</ span > , < span class ='ident '> line</ span > );
327
395
}</ pre >
328
396
329
- < p > Outputs:</ p >
397
+ <!-- Outputs: -->
398
+
399
+ < p > 出力:</ p >
330
400
331
401
< pre > < code class ="language-text "> 0: Content of line one
332
402
1: Content of line two
333
403
2: Content of line three
334
404
3: Content of line four
335
405
</ code > </ pre >
336
406
337
- < h2 id ='ending-iteration-early ' class ='section-header '> < a href ='#ending-iteration-early '> Ending iteration early</ a > </ h2 >
338
- < p > Let’s take a look at that < code > while</ code > loop we had earlier:</ p >
407
+ <!-- ## Ending iteration early -->
408
+
409
+ < h2 id ='反復の早期終了 ' class ='section-header '> < a href ='#反復の早期終了 '> 反復の早期終了</ a > </ h2 >
410
+ <!-- Let’s take a look at that `while` loop we had earlier: -->
411
+
412
+ < p > さきほどの < code > while</ code > ループを見てみましょう:</ p >
339
413
< span class ='rusttest '> fn main() {
340
414
let mut x = 5;
341
415
let mut done = false;
@@ -363,11 +437,18 @@ <h2 id='ending-iteration-early' class='section-header'><a href='#ending-iteratio
363
437
}
364
438
}</ pre >
365
439
366
- < p > We had to keep a dedicated < code > mut</ code > boolean variable binding, < code > done</ code > , to know
367
- when we should exit out of the loop. Rust has two keywords to help us with
368
- modifying iteration: < code > break</ code > and < code > continue</ code > .</ p >
440
+ <!-- We had to keep a dedicated `mut` boolean variable binding, `done`, to know -->
441
+
442
+ <!-- when we should exit out of the loop. Rust has two keywords to help us with -->
443
+
444
+ <!-- modifying iteration: `break` and `continue`. -->
369
445
370
- < p > In this case, we can write the loop in a better way with < code > break</ code > :</ p >
446
+ < p > ループをいつ終了すべきか知るため、ここでは専用の < code > mut</ code > なboolean変数束縛 < code > done</ code > を用いました。
447
+ Rustには反復の変更を手伝う2つキーワード: < code > break</ code > と < code > continue</ code > があります。</ p >
448
+
449
+ <!-- In this case, we can write the loop in a better way with `break`: -->
450
+
451
+ < p > この例では、 < code > break</ code > を使ってループを記述した方が良いでしょう:</ p >
371
452
< span class ='rusttest '> fn main() {
372
453
let mut x = 5;
373
454
@@ -389,10 +470,17 @@ <h2 id='ending-iteration-early' class='section-header'><a href='#ending-iteratio
389
470
< span class ='kw '> if</ span > < span class ='ident '> x</ span > < span class ='op '> %</ span > < span class ='number '> 5</ span > < span class ='op '> ==</ span > < span class ='number '> 0</ span > { < span class ='kw '> break</ span > ; }
390
471
}</ pre >
391
472
392
- < p > We now loop forever with < code > loop</ code > and use < code > break</ code > to break out early. Issuing an explicit < code > return</ code > statement will also serve to terminate the loop early.</ p >
473
+ <!-- We now loop forever with `loop` and use `break` to break out early. Issuing an explicit `return` statement will also serve to terminate the loop early. -->
474
+
475
+ < p > ここでは < code > loop</ code > による永久ループと < code > break</ code > による早期脱出を使っています。
476
+ 明示的な < code > return</ code > 文の発行でもループの早期終了になります。</ p >
477
+
478
+ <!-- `continue` is similar, but instead of ending the loop, goes to the next -->
393
479
394
- < p > < code > continue</ code > is similar, but instead of ending the loop, goes to the next
395
- iteration. This will only print the odd numbers:</ p >
480
+ <!-- iteration. This will only print the odd numbers: -->
481
+
482
+ < p > < code > continue</ code > も似ていますが、ループを終了させるのではなく、次の反復へと進めます。
483
+ これは奇数だけを表示するでしょう:</ p >
396
484
< span class ='rusttest '> fn main() {
397
485
for x in 0..10 {
398
486
if x % 2 == 0 { continue; }
@@ -406,27 +494,42 @@ <h2 id='ending-iteration-early' class='section-header'><a href='#ending-iteratio
406
494
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "{}"</ span > , < span class ='ident '> x</ span > );
407
495
}</ pre >
408
496
409
- < h2 id ='loop-labels ' class ='section-header '> < a href ='#loop-labels '> Loop labels</ a > </ h2 >
410
- < p > You may also encounter situations where you have nested loops and need to
411
- specify which one your < code > break</ code > or < code > continue</ code > statement is for. Like most
412
- other languages, by default a < code > break</ code > or < code > continue</ code > will apply to innermost
413
- loop. In a situation where you would like to a < code > break</ code > or < code > continue</ code > for one
414
- of the outer loops, you can use labels to specify which loop the < code > break</ code > or
415
- < code > continue</ code > statement applies to. This will only print when both < code > x</ code > and < code > y</ code > are
416
- odd:</ p >
497
+ <!-- ## Loop labels -->
498
+
499
+ < h2 id ='ループラベル ' class ='section-header '> < a href ='#ループラベル '> ループラベル</ a > </ h2 >
500
+ <!-- You may also encounter situations where you have nested loops and need to -->
501
+
502
+ <!-- specify which one your `break` or `continue` statement is for. Like most -->
503
+
504
+ <!-- other languages, by default a `break` or `continue` will apply to innermost -->
505
+
506
+ <!-- loop. In a situation where you would like to a `break` or `continue` for one -->
507
+
508
+ <!-- of the outer loops, you can use labels to specify which loop the `break` or -->
509
+
510
+ <!-- `continue` statement applies to. This will only print when both `x` and `y` are -->
511
+
512
+ <!-- odd: -->
513
+
514
+ < p > 入れ子のループがあり、< code > break</ code > や < code > continue</ code > 文がどのループに対応するか指定する必要がある、
515
+ そんな状況に出会うこともあるでしょう。大抵の他言語と同様に、 < code > break</ code > や < code > continue</ code > は最内ループに適用されるのがデフォルトです。
516
+ 外側のループに < code > break</ code > や < code > continue</ code > を使いたいという状況では、 < code > break</ code > や < code > continue</ code > 文の適用先を指定するラベルを使えます。
517
+ これは < code > x</ code > と < code > y</ code > 両方が奇数のときだけ表示を行います:</ p >
417
518
< span class ='rusttest '> fn main() {
418
519
'outer: for x in 0..10 {
419
520
'inner: for y in 0..10 {
420
- if x % 2 == 0 { continue 'outer; } // continues the loop over x
421
- if y % 2 == 0 { continue 'inner; } // continues the loop over y
521
+ // if x % 2 == 0 { continue 'outer; } // continues the loop over x
522
+ if x % 2 == 0 { continue 'outer; } // x のループを継続
523
+ // if y % 2 == 0 { continue 'inner; } // continues the loop over y
524
+ if y % 2 == 0 { continue 'inner; } // y のループを継続
422
525
println!("x: {}, y: {}", x, y);
423
526
}
424
527
}
425
528
}</ span > < pre class ='rust rust-example-rendered '>
426
529
< span class ='lifetime '> 'outer</ span > : < span class ='kw '> for</ span > < span class ='ident '> x</ span > < span class ='kw '> in</ span > < span class ='number '> 0</ span > ..< span class ='number '> 10</ span > {
427
530
< span class ='lifetime '> 'inner</ span > : < span class ='kw '> for</ span > < span class ='ident '> y</ span > < span class ='kw '> in</ span > < span class ='number '> 0</ span > ..< span class ='number '> 10</ span > {
428
- < span class ='kw '> if</ span > < span class ='ident '> x</ span > < span class ='op '> %</ span > < span class ='number '> 2</ span > < span class ='op '> ==</ span > < span class ='number '> 0</ span > { < span class ='kw '> continue</ span > < span class ='lifetime '> 'outer</ span > ; } < span class ='comment '> // continues the loop over x </ span >
429
- < span class ='kw '> if</ span > < span class ='ident '> y</ span > < span class ='op '> %</ span > < span class ='number '> 2</ span > < span class ='op '> ==</ span > < span class ='number '> 0</ span > { < span class ='kw '> continue</ span > < span class ='lifetime '> 'inner</ span > ; } < span class ='comment '> // continues the loop over y </ span >
531
+ < span class ='kw '> if</ span > < span class ='ident '> x</ span > < span class ='op '> %</ span > < span class ='number '> 2</ span > < span class ='op '> ==</ span > < span class ='number '> 0</ span > { < span class ='kw '> continue</ span > < span class ='lifetime '> 'outer</ span > ; } < span class ='comment '> // x のループを継続 </ span >
532
+ < span class ='kw '> if</ span > < span class ='ident '> y</ span > < span class ='op '> %</ span > < span class ='number '> 2</ span > < span class ='op '> ==</ span > < span class ='number '> 0</ span > { < span class ='kw '> continue</ span > < span class ='lifetime '> 'inner</ span > ; } < span class ='comment '> // y のループを継続 </ span >
430
533
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "x: {}, y: {}"</ span > , < span class ='ident '> x</ span > , < span class ='ident '> y</ span > );
431
534
}
432
535
}</ pre >
0 commit comments