Skip to content

4.6 - loop / ループ (1.9) #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions 1.9/en/book/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ Don't forget to add the parentheses around the range.
#### On iterators:

```rust
# let lines = "hello\nworld".lines();
let lines = "hello\nworld".lines();

for (linenumber, line) in lines.enumerate() {
println!("{}: {}", linenumber, line);
}
Expand All @@ -134,10 +135,8 @@ for (linenumber, line) in lines.enumerate() {
Outputs:

```text
0: Content of line one
1: Content of line two
2: Content of line three
3: Content of line four
0: hello
1: world
```

## Ending iteration early
Expand Down Expand Up @@ -195,7 +194,7 @@ for x in 0..10 {
You may also encounter situations where you have nested loops and need to
specify which one your `break` or `continue` statement is for. Like most
other languages, by default a `break` or `continue` will apply to innermost
loop. In a situation where you would like to a `break` or `continue` for one
loop. In a situation where you would like to `break` or `continue` for one
of the outer loops, you can use labels to specify which loop the `break` or
`continue` statement applies to. This will only print when both `x` and `y` are
odd:
Expand Down
59 changes: 27 additions & 32 deletions 1.9/ja/book/loops.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<!-- % Loops -->

<!-- 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. -->
なんらかの繰り返しを伴う処理に対して、Rust言語は3種類のアプローチ: `loop`, `while`, `for` を提供します
各アプローチにはそれぞれの使い道があります
現在、Rustは、なんらかの繰り返しを伴う処理に対して、3種類の手法: `loop`, `while`, `for` を提供しています
各アプローチにはそれぞれの使い方があります

## loop

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

```rust,ignore
loop {
Expand All @@ -20,7 +20,7 @@ loop {
## while

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

```rust
let mut x = 5; // mut x: i32
Expand All @@ -39,7 +39,7 @@ while !done {

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

<!-- If you need an infinite loop, you may be tempted to write this: -->
無限ループの必要があるとき、次のように書きたくなるかもしれません:
Expand All @@ -49,7 +49,7 @@ while true {
```

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

```rust,ignore
loop {
Expand All @@ -62,7 +62,7 @@ loop {
<!-- infinitely. -->
Rustの制御フロー解析では、必ずループすると知っていることから、これを `while true` とは異なる構造として扱います。
一般に、コンパイラへ与える情報量が多いほど、安全性が高くより良いコード生成につながるため、
無限にループするつもりなら常に `loop` を使うべきです。
無限にループするつもりであれば、常に `loop` を使うべきです。


## for
Expand All @@ -71,8 +71,7 @@ Rustの制御フロー解析では、必ずループすると知っているこ
<!-- work a bit differently than in other systems languages, however. Rust’s `for` -->
<!-- loop doesn’t look like this “C-style” `for` loop: -->

特定の回数だけループするときには `for` ループを使います。しかし、Rustの `for` ループは他のシステムプログラミング言語のそれとは少し異なる働きをします。
Rustの `for` ループは、次のような「Cスタイル」 `for` ループとは似ていません:
特定の回数だけループするときには `for` ループを使います。しかし、Rustの `for` ループは他のシステムプログラミング言語のそれとは少し異なる働きをします。 Rustの `for` ループは、次のような「Cスタイル」 `for` ループとは似ていません:

```c
for (x = 0; x < 10; x++) {
Expand Down Expand Up @@ -104,32 +103,29 @@ for var in expression {
<!-- valid for the loop body. Once the body is over, the next value is fetched from -->
<!-- the iterator, and we loop another time. When there are no more values, the `for` -->
<!-- loop is over. -->
式(expression)は[`IntoIterator`]を用いて[イテレータ][iterator]へと変換可能なアイテムです。
イテレータは要素の連なりを返します。それぞれの要素がループの1回の反復になります。
その値は名前 `var` に束縛されて、ループ本体にて有効になります。いったんループ本体を抜けると、
次の値がイテレータから取り出され、次のループ処理を行います。それ以上の値が存在しない時は、
`for` ループは終了します。
式(expression)は[`IntoIterator`]を用いて[イテレータ][iterator]へと変換することができるアイテムです。
イテレータは一連の要素を返します。それぞれの要素がループの1回の反復になります。 その値は、ループ本体に有効な名前, `var` に束縛されています。いったんループ本体を抜けると、次の値がイテレータから取り出され、次のループ処理を行います。それ以上の値が存在しない時は、`for` ループは終了します。

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

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

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

<!-- ### Enumerate -->
### 列挙

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

<!-- #### On ranges: -->
#### レンジを対象に:
Expand Down Expand Up @@ -158,7 +154,8 @@ i = 4 and j = 9
#### イテレータを対象に:

```rust
# let lines = "hello\nworld".lines();
let lines = "hello\nworld".lines();

for (linenumber, line) in lines.enumerate() {
println!("{}: {}", linenumber, line);
}
Expand All @@ -168,10 +165,8 @@ for (linenumber, line) in lines.enumerate() {
出力:

```text
0: Content of line one
1: Content of line two
2: Content of line three
3: Content of line four
0: hello
1: world
```

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

ループを終了する時を知るために、、専用の `mut`であるboolean変数束縛, `done` を使わなければなりませんでした。
Rustには反復の変更を手伝けする2つのキーワード: `break` と `continue` があります。

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

<!-- 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. -->
ここでは `loop` による永久ループと `break` による早期脱出を使っています
明示的な `return` 文の発行でもループの早期終了になります
ここでは `loop` による永久ループと 早期にループを抜けるため `break` を使っています
明示的な `return` 文の発行でもループを早期に終了します

<!-- `continue` is similar, but instead of ending the loop, goes to the next -->
<!-- iteration. This will only print the odd numbers: -->
Expand All @@ -239,14 +235,13 @@ for x in 0..10 {
<!-- You may also encounter situations where you have nested loops and need to -->
<!-- specify which one your `break` or `continue` statement is for. Like most -->
<!-- other languages, by default a `break` or `continue` will apply to innermost -->
<!-- loop. In a situation where you would like to a `break` or `continue` for one -->
<!-- loop. In a situation where you would like to `break` or `continue` for one -->
<!-- of the outer loops, you can use labels to specify which loop the `break` or -->
<!-- `continue` statement applies to. This will only print when both `x` and `y` are -->
<!-- odd: -->
入れ子のループがあり、`break` や `continue` 文がどのループに対応するか指定する必要がある、
そんな状況に出会うこともあるでしょう。大抵の他言語と同様に、 `break` や `continue` は最内ループに適用されるのがデフォルトです。
外側のループに `break` や `continue` を使いたいという状況では、 `break` や `continue` 文の適用先を指定するラベルを使えます。
これは `x` と `y` 両方が奇数のときだけ表示を行います:
そのような状況に出会うかもしれません。大抵の他言語と同様に、 デフォルトで `break` や `continue` は最内ループに適用されます。
外側のループに `break` や `continue` を使いたいという状況では、 `break` や `continue` 文の適用先を指定するラベルを使えます。これは `x` と `y` 両方がともに奇数のときだけ表示を行います:

```rust
'outer: for x in 0..10 {
Expand Down
35 changes: 0 additions & 35 deletions diff-1.6.0..1.9.0/src/doc/book/loops.md

This file was deleted.