From f839e51f5518a9721d87c6d2c4e899d1fdcd581d Mon Sep 17 00:00:00 2001 From: yoh Date: Sat, 6 Feb 2016 00:50:04 +0900 Subject: [PATCH 1/4] start to translate loop.md --- 1.6/ja/book/loops.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/1.6/ja/book/loops.md b/1.6/ja/book/loops.md index 68bb49d2..161b5772 100644 --- a/1.6/ja/book/loops.md +++ b/1.6/ja/book/loops.md @@ -1,4 +1,5 @@ -% 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. From d7a340496ef7fa69824e211a2f2961f21083d11c Mon Sep 17 00:00:00 2001 From: yoh Date: Mon, 8 Feb 2016 00:31:55 +0900 Subject: [PATCH 2/4] translate loop.md --- 1.6/ja/book/loops.md | 161 ++++++++++++++++++++++++++++--------------- 1 file changed, 105 insertions(+), 56 deletions(-) diff --git a/1.6/ja/book/loops.md b/1.6/ja/book/loops.md index 161b5772..13a57576 100644 --- a/1.6/ja/book/loops.md +++ b/1.6/ja/book/loops.md @@ -1,21 +1,27 @@ % ループ -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` を提供します。 +各アプローチにはそれぞれの使い道があります。 ## 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,ignore loop { - println!("Loop forever!"); +# // println!("Loop forever!"); + println!("無限ループ!"); } ``` ## while -Rust also has a `while` loop. It looks like this: + +Rustには `while` ループもあります。このように: ```rust let mut x = 5; // mut x: i32 @@ -32,32 +38,42 @@ while !done { } ``` -`while` loops are the correct choice when you’re not sure how many times -you need to loop. + + +何回ループする必要があるか明らかではない状況で、`while` ループは正しい選択肢です。 -If you need an infinite loop, you may be tempted to write this: + +無限ループの必要があるとき、次のように書きたくなるかもしれません: ```rust,ignore while true { ``` -However, `loop` is far better suited to handle this case: + +しかし、このような状況では `loop` の方がずっと適しています。 ```rust,ignore loop { ``` -Rust’s control-flow analysis treats this construct differently than a `while -true`, since we know that it will always loop. In general, the more information -we can give to the compiler, the better it can do with safety and code -generation, so you should always prefer `loop` when you plan to loop -infinitely. + + + + + +Rustの制御フロー解析では、必ずループが行われることから、これを `while true` とは異なる構造として扱います。 +一般に、コンパイラに与えられる情報量が多いほど、安全性が高くより良いコード生成につながるため、 +無限ループを行いたいならば常に `loop` を使うべきなのです。 + ## for -The `for` loop is used to loop a particular number of times. Rust’s `for` loops -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` ループとは似ていません: ```c for (x = 0; x < 10; x++) { @@ -65,7 +81,8 @@ for (x = 0; x < 10; x++) { } ``` -Instead, it looks like this: + +代わりに、このように書きます: ```rust for x in 0..10 { @@ -73,7 +90,8 @@ for x in 0..10 { } ``` -In slightly more abstract terms, + +もう少し抽象的な用語を使うと、 ```ignore for var in expression { @@ -81,29 +99,41 @@ for var in expression { } ``` -The expression is an item that can be converted into an [iterator] using -[`IntoIterator`]. The iterator gives back a series of elements. Each element is -one iteration of the loop. That value is then bound to the name `var`, which is -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` ループは終了します。 [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` ではありません。 -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開発者でさえ、 +ループの各要素を手動制御することは複雑であり、また間違いを犯しやすいのです。 -### Enumerate + +### 列挙 -When you need to keep track of how many times you already looped, you can use the `.enumerate()` function. + +ループ中で何回目の繰り返しかを知る必要があるなら、 `.enumerate()` 関数が使えます。 -#### On ranges: + +####️⃣ 範囲(range)を対象に: ```rust for (i,j) in (5..10).enumerate() { @@ -111,7 +141,8 @@ for (i,j) in (5..10).enumerate() { } ``` -Outputs: + +出力: ```text i = 0 and j = 5 @@ -121,9 +152,11 @@ i = 3 and j = 8 i = 4 and j = 9 ``` -Don't forget to add the parentheses around the range. + +範囲を括弧で囲うことを忘れないで。 -#### On iterators: + +#### イテレータを対象に: ```rust # let lines = "hello\nworld".lines(); @@ -132,7 +165,8 @@ for (linenumber, line) in lines.enumerate() { } ``` -Outputs: + +出力: ```text 0: Content of line one @@ -141,9 +175,11 @@ Outputs: 3: Content of line four ``` -## Ending iteration early + +## 反復の早期終了 -Let’s take a look at that `while` loop we had earlier: + +早期終了を伴う `while` ループを見てみましょう: ```rust let mut x = 5; @@ -160,11 +196,14 @@ 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` です。 -In this case, we can write the loop in a better way with `break`: + +今回は、 `break` を使ってループを記述するのが良い方法です: ```rust let mut x = 5; @@ -178,10 +217,14 @@ 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` 文の発行でもループの早期終了になります。 -`continue` is similar, but instead of ending the loop, goes to the next -iteration. This will only print the odd numbers: + + +`continue` も似ていますが、ループを終了させるのではなく、次の反復へと進めます。 +これは奇数だけを表示するでしょう: ```rust for x in 0..10 { @@ -191,21 +234,27 @@ for x in 0..10 { } ``` -## Loop labels + +## ループラベル -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 -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` 両方が奇数のときだけ表示します: ```rust 'outer: for x in 0..10 { 'inner: for y in 0..10 { - if x % 2 == 0 { continue 'outer; } // continues the loop over x - if y % 2 == 0 { continue 'inner; } // continues the loop over y +# // if x % 2 == 0 { continue 'outer; } // continues the loop over x + if x % 2 == 0 { continue 'outer; } // x のループを継続 +# // if y % 2 == 0 { continue 'inner; } // continues the loop over y + if y % 2 == 0 { continue 'inner; } // y のループを継続 println!("x: {}, y: {}", x, y); } } From 6ca764bd283c1f450d7cfbb35d3570aec26eb9fb Mon Sep 17 00:00:00 2001 From: yoh Date: Mon, 8 Feb 2016 12:51:30 +0900 Subject: [PATCH 3/4] fix translation - apply self-review - small refinement --- 1.6/ja/book/loops.md | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/1.6/ja/book/loops.md b/1.6/ja/book/loops.md index 13a57576..61b4f143 100644 --- a/1.6/ja/book/loops.md +++ b/1.6/ja/book/loops.md @@ -9,12 +9,12 @@ Rustで使えるループなかで最もシンプルな形式が、無限 `loop` です。Rustのキーワード `loop` によって、 -何らかの終了状態に到達するまでずっとループし続ける手段を提供します。 +何らかの終了状態に到達するまでずっとループし続ける手段を提供します。Rustの無限 `loop` はこのように: ```rust,ignore loop { # // println!("Loop forever!"); - println!("無限ループ!"); + println!("永久にループ!"); } ``` @@ -50,7 +50,7 @@ while true { ``` -しかし、このような状況では `loop` の方がずっと適しています。 +しかし、こういった場合には `loop` の方がずっと適しています。 ```rust,ignore loop { @@ -61,9 +61,9 @@ loop { -Rustの制御フロー解析では、必ずループが行われることから、これを `while true` とは異なる構造として扱います。 -一般に、コンパイラに与えられる情報量が多いほど、安全性が高くより良いコード生成につながるため、 -無限ループを行いたいならば常に `loop` を使うべきなのです。 +Rustの制御フロー解析では、必ずループすると知っていることから、これを `while true` とは異なる構造として扱います。 +一般に、コンパイラへ与える情報量が多いほど、安全性が高くより良いコード生成につながるため、 +無限にループするつもりなら常に `loop` を使うべきです。 ## for @@ -133,7 +133,7 @@ Rustでは意図的に「Cスタイル」 `for` ループを持ちません。 ループ中で何回目の繰り返しかを知る必要があるなら、 `.enumerate()` 関数が使えます。 -####️⃣ 範囲(range)を対象に: +#### レンジを対象に: ```rust for (i,j) in (5..10).enumerate() { @@ -153,7 +153,7 @@ i = 4 and j = 9 ``` -範囲を括弧で囲うことを忘れないで。 +レンジを括弧で囲うのを忘れないで下さい。 #### イテレータを対象に: @@ -179,7 +179,7 @@ for (linenumber, line) in lines.enumerate() { ## 反復の早期終了 -早期終了を伴う `while` ループを見てみましょう: +さきほどの `while` ループを見てみましょう: ```rust let mut x = 5; @@ -199,11 +199,11 @@ while !done { -ループをいつ終了すべきか知るために、ここでは専用の `mut` なboolean変数束縛 `done` を用います。 -Rustには繰り返しの変更を手伝う2つキーワードがあります: `break` と `continue` です。 +ループをいつ終了すべきか知るため、ここでは専用の `mut` なboolean変数束縛 `done` を用いました。 +Rustには反復の変更を手伝う2つキーワード: `break` と `continue` があります。 -今回は、 `break` を使ってループを記述するのが良い方法です: +この例では、 `break` を使ってループを記述した方が良いでしょう: ```rust let mut x = 5; @@ -218,7 +218,7 @@ loop { ``` -ここでは `loop` による無限ループと `break` による早期脱出を使っています。 +ここでは `loop` による永久ループと `break` による早期脱出を使っています。 明示的な `return` 文の発行でもループの早期終了になります。 @@ -245,8 +245,9 @@ for x in 0..10 { 入れ子のループがあり、`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 { From 6a6ce8f2824d079114b8eca06795853e3e773683 Mon Sep 17 00:00:00 2001 From: yoh Date: Wed, 10 Feb 2016 12:54:09 +0900 Subject: [PATCH 4/4] revert translation(ja->en) in string literal --- 1.6/ja/book/loops.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/1.6/ja/book/loops.md b/1.6/ja/book/loops.md index 61b4f143..fa641732 100644 --- a/1.6/ja/book/loops.md +++ b/1.6/ja/book/loops.md @@ -13,8 +13,7 @@ Rustで使えるループなかで最もシンプルな形式が、無限 `loop` ```rust,ignore loop { -# // println!("Loop forever!"); - println!("永久にループ!"); + println!("Loop forever!"); } ```