Skip to content

Commit acc273f

Browse files
committed
Merge pull request #71 from yohhoy/loop
4.6 Loops
2 parents 1b92cef + 6a6ce8f commit acc273f

File tree

1 file changed

+107
-57
lines changed

1 file changed

+107
-57
lines changed

1.6/ja/book/loops.md

Lines changed: 107 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
% Loops
1+
% ループ
2+
<!-- % Loops -->
23

3-
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.
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+
各アプローチにはそれぞれの使い道があります。
47

58
## loop
69

7-
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:
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+
Rustで使えるループなかで最もシンプルな形式が、無限 `loop` です。Rustのキーワード `loop` によって、
12+
何らかの終了状態に到達するまでずっとループし続ける手段を提供します。Rustの無限 `loop` はこのように:
813

914
```rust,ignore
1015
loop {
@@ -14,7 +19,8 @@ loop {
1419

1520
## while
1621

17-
Rust also has a `while` loop. It looks like this:
22+
<!-- Rust also has a `while` loop. It looks like this: -->
23+
Rustには `while` ループもあります。このように:
1824

1925
```rust
2026
let mut x = 5; // mut x: i32
@@ -31,86 +37,111 @@ while !done {
3137
}
3238
```
3339

34-
`while` loops are the correct choice when you’re not sure how many times
35-
you need to loop.
40+
<!-- `while` loops are the correct choice when you’re not sure how many times -->
41+
<!-- you need to loop. -->
42+
何回ループする必要があるか明らかではない状況で、`while` ループは正しい選択肢です。
3643

37-
If you need an infinite loop, you may be tempted to write this:
44+
<!-- If you need an infinite loop, you may be tempted to write this: -->
45+
無限ループの必要があるとき、次のように書きたくなるかもしれません:
3846

3947
```rust,ignore
4048
while true {
4149
```
4250

43-
However, `loop` is far better suited to handle this case:
51+
<!-- However, `loop` is far better suited to handle this case: -->
52+
しかし、こういった場合には `loop` の方がずっと適しています。
4453

4554
```rust,ignore
4655
loop {
4756
```
4857

49-
Rust’s control-flow analysis treats this construct differently than a `while
50-
true`, since we know that it will always loop. In general, the more information
51-
we can give to the compiler, the better it can do with safety and code
52-
generation, so you should always prefer `loop` when you plan to loop
53-
infinitely.
58+
<!-- Rust’s control-flow analysis treats this construct differently than a `while -->
59+
<!-- true`, since we know that it will always loop. In general, the more information -->
60+
<!-- we can give to the compiler, the better it can do with safety and code -->
61+
<!-- generation, so you should always prefer `loop` when you plan to loop -->
62+
<!-- infinitely. -->
63+
Rustの制御フロー解析では、必ずループすると知っていることから、これを `while true` とは異なる構造として扱います。
64+
一般に、コンパイラへ与える情報量が多いほど、安全性が高くより良いコード生成につながるため、
65+
無限にループするつもりなら常に `loop` を使うべきです。
66+
5467

5568
## for
5669

57-
The `for` loop is used to loop a particular number of times. Rust’s `for` loops
58-
work a bit differently than in other systems languages, however. Rust’s `for`
59-
loop doesn’t look like this “C-style” `for` loop:
70+
<!-- The `for` loop is used to loop a particular number of times. Rust’s `for` loops -->
71+
<!-- work a bit differently than in other systems languages, however. Rust’s `for` -->
72+
<!-- loop doesn’t look like this “C-style” `for` loop: -->
73+
74+
特定の回数だけループするときには `for` ループを使います。しかし、Rustの `for` ループは他のシステムプログラミング言語のそれとは少し異なる働きをします。
75+
Rustの `for` ループは、次のような「Cスタイル」 `for` ループとは似ていません:
6076

6177
```c
6278
for (x = 0; x < 10; x++) {
6379
printf( "%d\n", x );
6480
}
6581
```
6682

67-
Instead, it looks like this:
83+
<!-- Instead, it looks like this: -->
84+
代わりに、このように書きます:
6885

6986
```rust
7087
for x in 0..10 {
7188
println!("{}", x); // x: i32
7289
}
7390
```
7491

75-
In slightly more abstract terms,
92+
<!-- In slightly more abstract terms, -->
93+
もう少し抽象的な用語を使うと、
7694

7795
```ignore
7896
for var in expression {
7997
code
8098
}
8199
```
82100

83-
The expression is an item that can be converted into an [iterator] using
84-
[`IntoIterator`]. The iterator gives back a series of elements. Each element is
85-
one iteration of the loop. That value is then bound to the name `var`, which is
86-
valid for the loop body. Once the body is over, the next value is fetched from
87-
the iterator, and we loop another time. When there are no more values, the `for`
88-
loop is over.
101+
<!-- The expression is an item that can be converted into an [iterator] using -->
102+
<!-- [`IntoIterator`]. The iterator gives back a series of elements. Each element is -->
103+
<!-- one iteration of the loop. That value is then bound to the name `var`, which is -->
104+
<!-- valid for the loop body. Once the body is over, the next value is fetched from -->
105+
<!-- the iterator, and we loop another time. When there are no more values, the `for` -->
106+
<!-- loop is over. -->
107+
式(expression)は[`IntoIterator`]を用いて[イテレータ][iterator]へと変換可能なアイテムです。
108+
イテレータは要素の連なりを返します。それぞれの要素がループの1回の反復になります。
109+
その値は名前 `var` に束縛されて、ループ本体にて有効になります。いったんループ本体を抜けると、
110+
次の値がイテレータから取り出され、次のループ処理を行います。それ以上の値が存在しない時は、
111+
`for` ループは終了します。
89112

90113
[iterator]: iterators.html
91114
[`IntoIterator`]: ../std/iter/trait.IntoIterator.html
92115

93-
In our example, `0..10` is an expression that takes a start and an end position,
94-
and gives an iterator over those values. The upper bound is exclusive, though,
95-
so our loop will print `0` through `9`, not `10`.
116+
<!-- In our example, `0..10` is an expression that takes a start and an end position, -->
117+
<!-- and gives an iterator over those values. The upper bound is exclusive, though, -->
118+
<!-- so our loop will print `0` through `9`, not `10`. -->
119+
例示では、`0..10` が開始位置と終了位置をとる式であり、同範囲の値を返すイテレータを与えます。
120+
上界はその値自身を含まないため、このループは `0` から `9` までを表示します。 `10` ではありません。
96121

97-
Rust does not have the “C-style” `for` loop on purpose. Manually controlling
98-
each element of the loop is complicated and error prone, even for experienced C
99-
developers.
122+
<!-- Rust does not have the “C-style” `for` loop on purpose. Manually controlling -->
123+
<!-- each element of the loop is complicated and error prone, even for experienced C -->
124+
<!-- developers. -->
125+
Rustでは意図的に「Cスタイル」 `for` ループを持ちません。経験豊富なC開発者でさえ、
126+
ループの各要素を手動制御することは複雑であり、また間違いを犯しやすいのです。
100127

101-
### Enumerate
128+
<!-- ### Enumerate -->
129+
### 列挙
102130

103-
When you need to keep track of how many times you already looped, you can use the `.enumerate()` function.
131+
<!-- When you need to keep track of how many times you already looped, you can use the `.enumerate()` function. -->
132+
ループ中で何回目の繰り返しかを知る必要があるなら、 `.enumerate()` 関数が使えます。
104133

105-
#### On ranges:
134+
<!-- #### On ranges: -->
135+
#### レンジを対象に:
106136

107137
```rust
108138
for (i,j) in (5..10).enumerate() {
109139
println!("i = {} and j = {}", i, j);
110140
}
111141
```
112142

113-
Outputs:
143+
<!-- Outputs: -->
144+
出力:
114145

115146
```text
116147
i = 0 and j = 5
@@ -120,9 +151,11 @@ i = 3 and j = 8
120151
i = 4 and j = 9
121152
```
122153

123-
Don't forget to add the parentheses around the range.
154+
<!-- Don't forget to add the parentheses around the range. -->
155+
レンジを括弧で囲うのを忘れないで下さい。
124156

125-
#### On iterators:
157+
<!-- #### On iterators: -->
158+
#### イテレータを対象に:
126159

127160
```rust
128161
# let lines = "hello\nworld".lines();
@@ -131,7 +164,8 @@ for (linenumber, line) in lines.enumerate() {
131164
}
132165
```
133166

134-
Outputs:
167+
<!-- Outputs: -->
168+
出力:
135169

136170
```text
137171
0: Content of line one
@@ -140,9 +174,11 @@ Outputs:
140174
3: Content of line four
141175
```
142176

143-
## Ending iteration early
177+
<!-- ## Ending iteration early -->
178+
## 反復の早期終了
144179

145-
Let’s take a look at that `while` loop we had earlier:
180+
<!-- Let’s take a look at that `while` loop we had earlier: -->
181+
さきほどの `while` ループを見てみましょう:
146182

147183
```rust
148184
let mut x = 5;
@@ -159,11 +195,14 @@ while !done {
159195
}
160196
```
161197

162-
We had to keep a dedicated `mut` boolean variable binding, `done`, to know
163-
when we should exit out of the loop. Rust has two keywords to help us with
164-
modifying iteration: `break` and `continue`.
198+
<!-- We had to keep a dedicated `mut` boolean variable binding, `done`, to know -->
199+
<!-- when we should exit out of the loop. Rust has two keywords to help us with -->
200+
<!-- modifying iteration: `break` and `continue`. -->
201+
ループをいつ終了すべきか知るため、ここでは専用の `mut` なboolean変数束縛 `done` を用いました。
202+
Rustには反復の変更を手伝う2つキーワード: `break``continue` があります。
165203

166-
In this case, we can write the loop in a better way with `break`:
204+
<!-- In this case, we can write the loop in a better way with `break`: -->
205+
この例では、 `break` を使ってループを記述した方が良いでしょう:
167206

168207
```rust
169208
let mut x = 5;
@@ -177,10 +216,14 @@ loop {
177216
}
178217
```
179218

180-
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.
219+
<!-- 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` 文の発行でもループの早期終了になります。
181222

182-
`continue` is similar, but instead of ending the loop, goes to the next
183-
iteration. This will only print the odd numbers:
223+
<!-- `continue` is similar, but instead of ending the loop, goes to the next -->
224+
<!-- iteration. This will only print the odd numbers: -->
225+
`continue` も似ていますが、ループを終了させるのではなく、次の反復へと進めます。
226+
これは奇数だけを表示するでしょう:
184227

185228
```rust
186229
for x in 0..10 {
@@ -190,21 +233,28 @@ for x in 0..10 {
190233
}
191234
```
192235

193-
## Loop labels
194-
195-
You may also encounter situations where you have nested loops and need to
196-
specify which one your `break` or `continue` statement is for. Like most
197-
other languages, by default a `break` or `continue` will apply to innermost
198-
loop. In a situation where you would like to a `break` or `continue` for one
199-
of the outer loops, you can use labels to specify which loop the `break` or
200-
`continue` statement applies to. This will only print when both `x` and `y` are
201-
odd:
236+
<!-- ## Loop labels -->
237+
## ループラベル
238+
239+
<!-- You may also encounter situations where you have nested loops and need to -->
240+
<!-- specify which one your `break` or `continue` statement is for. Like most -->
241+
<!-- 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 -->
243+
<!-- of the outer loops, you can use labels to specify which loop the `break` or -->
244+
<!-- `continue` statement applies to. This will only print when both `x` and `y` are -->
245+
<!-- odd: -->
246+
入れ子のループがあり、`break``continue` 文がどのループに対応するか指定する必要がある、
247+
そんな状況に出会うこともあるでしょう。大抵の他言語と同様に、 `break``continue` は最内ループに適用されるのがデフォルトです。
248+
外側のループに `break``continue` を使いたいという状況では、 `break``continue` 文の適用先を指定するラベルを使えます。
249+
これは `x``y` 両方が奇数のときだけ表示を行います:
202250

203251
```rust
204252
'outer: for x in 0..10 {
205253
'inner: for y in 0..10 {
206-
if x % 2 == 0 { continue 'outer; } // continues the loop over x
207-
if y % 2 == 0 { continue 'inner; } // continues the loop over y
254+
# // if x % 2 == 0 { continue 'outer; } // continues the loop over x
255+
if x % 2 == 0 { continue 'outer; } // x のループを継続
256+
# // if y % 2 == 0 { continue 'inner; } // continues the loop over y
257+
if y % 2 == 0 { continue 'inner; } // y のループを継続
208258
println!("x: {}, y: {}", x, y);
209259
}
210260
}

0 commit comments

Comments
 (0)