2
2
<!-- % Loops -->
3
3
4
4
<!-- 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. -->
5
- なんらかの繰り返しを伴う処理に対して、Rust言語は3種類のアプローチ : ` loop ` , ` while ` , ` for ` を提供します 。
6
- 各アプローチにはそれぞれの使い道があります 。
5
+ 現在、Rustは、 なんらかの繰り返しを伴う処理に対して、3種類の手法 : ` loop ` , ` while ` , ` for ` を提供しています 。
6
+ 各アプローチにはそれぞれの使い方があります 。
7
7
8
8
## loop
9
9
10
10
<!-- 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: -->
11
11
Rustで使えるループのなかで最もシンプルな形式が、無限 ` loop ` です。Rustのキーワード ` loop ` によって、
12
- 何らかの終了状態に到達するまでずっとループし続ける手段を提供します 。Rustの無限 ` loop ` はこのように :
12
+ 何らかの終了状態に到達するまで 延々とループし続ける手段を提供します 。Rustの無限 ` loop ` は次の通りです :
13
13
14
14
``` rust,ignore
15
15
loop {
20
20
## while
21
21
22
22
<!-- Rust also has a `while` loop. It looks like this: -->
23
- Rustには ` while ` ループもあります。このように :
23
+ Rustには ` while ` ループもあります。次の通りです :
24
24
25
25
``` rust
26
26
let mut x = 5 ; // mut x: i32
@@ -39,7 +39,7 @@ while !done {
39
39
40
40
<!-- `while` loops are the correct choice when you’re not sure how many times -->
41
41
<!-- you need to loop. -->
42
- 何回ループする必要があるか明らかではない状況で 、` while ` ループは正しい選択肢です 。
42
+ 何回ループする必要があるか明らかではない状況では 、` while ` ループは正しい選択です 。
43
43
44
44
<!-- If you need an infinite loop, you may be tempted to write this: -->
45
45
無限ループの必要があるとき、次のように書きたくなるかもしれません:
@@ -49,7 +49,7 @@ while true {
49
49
```
50
50
51
51
<!-- However, `loop` is far better suited to handle this case: -->
52
- しかし、こういった場合には ` loop ` の方がずっと適しています 。
52
+ しかし、 ` loop ` は、 こういった場合に はるかに適しています 。
53
53
54
54
``` rust,ignore
55
55
loop {
62
62
<!-- infinitely. -->
63
63
Rustの制御フロー解析では、必ずループすると知っていることから、これを ` while true ` とは異なる構造として扱います。
64
64
一般に、コンパイラへ与える情報量が多いほど、安全性が高くより良いコード生成につながるため、
65
- 無限にループするつもりなら常に ` loop ` を使うべきです。
65
+ 無限にループするつもりであれば、常に ` loop ` を使うべきです。
66
66
67
67
68
68
## for
@@ -71,8 +71,7 @@ Rustの制御フロー解析では、必ずループすると知っているこ
71
71
<!-- work a bit differently than in other systems languages, however. Rust’s `for` -->
72
72
<!-- loop doesn’t look like this “C-style” `for` loop: -->
73
73
74
- 特定の回数だけループするときには ` for ` ループを使います。しかし、Rustの ` for ` ループは他のシステムプログラミング言語のそれとは少し異なる働きをします。
75
- Rustの ` for ` ループは、次のような「Cスタイル」 ` for ` ループとは似ていません:
74
+ 特定の回数だけループするときには ` for ` ループを使います。しかし、Rustの ` for ` ループは他のシステムプログラミング言語のそれとは少し異なる働きをします。 Rustの ` for ` ループは、次のような「Cスタイル」 ` for ` ループとは似ていません:
76
75
77
76
``` c
78
77
for (x = 0 ; x < 10 ; x++) {
@@ -104,32 +103,29 @@ for var in expression {
104
103
<!-- valid for the loop body. Once the body is over, the next value is fetched from -->
105
104
<!-- the iterator, and we loop another time. When there are no more values, the `for` -->
106
105
<!-- loop is over. -->
107
- 式(expression)は[ ` IntoIterator ` ] を用いて[ イテレータ] [ iterator ] へと変換可能なアイテムです。
108
- イテレータは要素の連なりを返します。それぞれの要素がループの1回の反復になります。
109
- その値は名前 ` var ` に束縛されて、ループ本体にて有効になります。いったんループ本体を抜けると、
110
- 次の値がイテレータから取り出され、次のループ処理を行います。それ以上の値が存在しない時は、
111
- ` for ` ループは終了します。
106
+ 式(expression)は[ ` IntoIterator ` ] を用いて[ イテレータ] [ iterator ] へと変換することができるアイテムです。
107
+ イテレータは一連の要素を返します。それぞれの要素がループの1回の反復になります。 その値は、ループ本体に有効な名前, ` var ` に束縛されています。いったんループ本体を抜けると、次の値がイテレータから取り出され、次のループ処理を行います。それ以上の値が存在しない時は、` for ` ループは終了します。
112
108
113
109
[ iterator ] : iterators.html
114
110
[ `IntoIterator` ] : ../std/iter/trait.IntoIterator.html
115
111
116
112
<!-- In our example, `0..10` is an expression that takes a start and an end position, -->
117
113
<!-- and gives an iterator over those values. The upper bound is exclusive, though, -->
118
114
<!-- so our loop will print `0` through `9`, not `10`. -->
119
- 例示では 、` 0..10 ` が開始位置と終了位置をとる式であり 、同範囲の値を返すイテレータを与えます。
120
- 上界はその値自身を含まないため 、このループは ` 0 ` から ` 9 ` までを表示します。 ` 10 ` ではありません。
115
+ この例では 、` 0..10 ` が開始と終了位置をとる式であり 、同範囲の値を返すイテレータを与えます。
116
+ 上限はその値自身を含まないため 、このループは ` 0 ` から ` 9 ` までを表示します。 ` 10 ` ではありません。
121
117
122
118
<!-- Rust does not have the “C-style” `for` loop on purpose. Manually controlling -->
123
119
<!-- each element of the loop is complicated and error prone, even for experienced C -->
124
120
<!-- developers. -->
125
- Rustでは意図的に「Cスタイル」 ` for ` ループを持ちません。経験豊富なC開発者でさえ 、
126
- ループの各要素を手動制御することは複雑であり 、また間違いを犯しやすいのです。
121
+ Rustでは意図的に「Cスタイル」 ` for ` ループを持ちません。経験豊富なC言語の開発者でさえ 、
122
+ ループの各要素を手動で制御することは複雑であり 、また間違いを犯しやすいのです。
127
123
128
124
<!-- ### Enumerate -->
129
125
### 列挙
130
126
131
127
<!-- When you need to keep track of how many times you already looped, you can use the `.enumerate()` function. -->
132
- ループ中で何回目の繰り返しかを知る必要があるなら 、 ` .enumerate() ` 関数が使えます。
128
+ ループの中で何回目の繰り返しかを把握する必要がある時 、 ` .enumerate() ` 関数が使えます。
133
129
134
130
<!-- #### On ranges: -->
135
131
#### レンジを対象に:
@@ -158,7 +154,8 @@ i = 4 and j = 9
158
154
#### イテレータを対象に:
159
155
160
156
``` rust
161
- # let lines = " hello\ n world" . lines ();
157
+ let lines = " hello\ n world" . lines ();
158
+
162
159
for (linenumber , line ) in lines . enumerate () {
163
160
println! (" {}: {}" , linenumber , line );
164
161
}
@@ -168,10 +165,8 @@ for (linenumber, line) in lines.enumerate() {
168
165
出力:
169
166
170
167
``` text
171
- 0: Content of line one
172
- 1: Content of line two
173
- 2: Content of line three
174
- 3: Content of line four
168
+ 0: hello
169
+ 1: world
175
170
```
176
171
177
172
<!-- ## Ending iteration early -->
@@ -198,8 +193,9 @@ while !done {
198
193
<!-- We had to keep a dedicated `mut` boolean variable binding, `done`, to know -->
199
194
<!-- when we should exit out of the loop. Rust has two keywords to help us with -->
200
195
<!-- modifying iteration: `break` and `continue`. -->
201
- ループをいつ終了すべきか知るため、ここでは専用の ` mut ` なboolean変数束縛 ` done ` を用いました。
202
- Rustには反復の変更を手伝う2つキーワード: ` break ` と ` continue ` があります。
196
+
197
+ ループを終了する時を知るために、、専用の ` mut ` であるboolean変数束縛, ` done ` を使わなければなりませんでした。
198
+ Rustには反復の変更を手伝けする2つのキーワード: ` break ` と ` continue ` があります。
203
199
204
200
<!-- In this case, we can write the loop in a better way with `break`: -->
205
201
この例では、 ` break ` を使ってループを記述した方が良いでしょう:
@@ -217,8 +213,8 @@ loop {
217
213
```
218
214
219
215
<!-- 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. -->
220
- ここでは ` loop ` による永久ループと ` break ` による早期脱出を使っています 。
221
- 明示的な ` return ` 文の発行でもループの早期終了になります 。
216
+ ここでは ` loop ` による永久ループと 早期にループを抜けるため ` break ` を使っています 。
217
+ 明示的な ` return ` 文の発行でもループを早期に終了します 。
222
218
223
219
<!-- `continue` is similar, but instead of ending the loop, goes to the next -->
224
220
<!-- iteration. This will only print the odd numbers: -->
@@ -239,14 +235,13 @@ for x in 0..10 {
239
235
<!-- You may also encounter situations where you have nested loops and need to -->
240
236
<!-- specify which one your `break` or `continue` statement is for. Like most -->
241
237
<!-- other languages, by default a `break` or `continue` will apply to innermost -->
242
- <!-- loop. In a situation where you would like to a `break` or `continue` for one -->
238
+ <!-- loop. In a situation where you would like to `break` or `continue` for one -->
243
239
<!-- of the outer loops, you can use labels to specify which loop the `break` or -->
244
240
<!-- `continue` statement applies to. This will only print when both `x` and `y` are -->
245
241
<!-- odd: -->
246
242
入れ子のループがあり、` break ` や ` continue ` 文がどのループに対応するか指定する必要がある、
247
- そんな状況に出会うこともあるでしょう。大抵の他言語と同様に、 ` break ` や ` continue ` は最内ループに適用されるのがデフォルトです。
248
- 外側のループに ` break ` や ` continue ` を使いたいという状況では、 ` break ` や ` continue ` 文の適用先を指定するラベルを使えます。
249
- これは ` x ` と ` y ` 両方が奇数のときだけ表示を行います:
243
+ そのような状況に出会うかもしれません。大抵の他言語と同様に、 デフォルトで ` break ` や ` continue ` は最内ループに適用されます。
244
+ 外側のループに ` break ` や ` continue ` を使いたいという状況では、 ` break ` や ` continue ` 文の適用先を指定するラベルを使えます。これは ` x ` と ` y ` 両方がともに奇数のときだけ表示を行います:
250
245
251
246
``` rust
252
247
'outer : for x in 0 .. 10 {
0 commit comments