Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7e2d575

Browse files
committedFeb 10, 2016
generate
1 parent acc273f commit 7e2d575

File tree

1 file changed

+161
-58
lines changed

1 file changed

+161
-58
lines changed
 

‎public/1.6/book/loops.html

Lines changed: 161 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta name="generator" content="rustdoc">
7-
<title>Loops</title>
7+
<title>ループ</title>
88

99
<link rel="stylesheet" type="text/css" href="rustbook.css">
1010

@@ -185,11 +185,19 @@
185185
<div id='page'>
186186

187187

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>
190195

191196
<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&#39;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>
193201
<span class='rusttest'>fn main() {
194202
loop {
195203
println!(&quot;Loop forever!&quot;);
@@ -200,7 +208,9 @@ <h2 id='loop' class='section-header'><a href='#loop'>loop</a></h2>
200208
}</pre>
201209

202210
<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>
204214
<span class='rusttest'>fn main() {
205215
let mut x = 5; // mut x: i32
206216
let mut done = false; // mut done: bool
@@ -228,37 +238,60 @@ <h2 id='while' class='section-header'><a href='#while'>while</a></h2>
228238
}
229239
}</pre>
230240

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: -->
233248

234-
<p>If you need an infinite loop, you may be tempted to write this:</p>
249+
<p>無限ループの必要があるとき、次のように書きたくなるかもしれません:</p>
235250
<span class='rusttest'>fn main() {
236251
while true {
237252
}</span><pre class='rust rust-example-rendered'>
238253
<span class='kw'>while</span> <span class='boolval'>true</span> {</pre>
239254

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>
241258
<span class='rusttest'>fn main() {
242259
loop {
243260
}</span><pre class='rust rust-example-rendered'>
244261
<span class='kw'>loop</span> {</pre>
245262

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>
250276

251277
<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>
255286

256287
<pre><code class="language-c">for (x = 0; x &lt; 10; x++) {
257288
printf( &quot;%d\n&quot;, x );
258289
}
259290
</code></pre>
260291

261-
<p>Instead, it looks like this:</p>
292+
<!-- Instead, it looks like this: -->
293+
294+
<p>代わりに、このように書きます:</p>
262295
<span class='rusttest'>fn main() {
263296
for x in 0..10 {
264297
println!(&quot;{}&quot;, x); // x: i32
@@ -268,7 +301,9 @@ <h2 id='for' class='section-header'><a href='#for'>for</a></h2>
268301
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{}&quot;</span>, <span class='ident'>x</span>); <span class='comment'>// x: i32</span>
269302
}</pre>
270303

271-
<p>In slightly more abstract terms,</p>
304+
<!-- In slightly more abstract terms, -->
305+
306+
<p>もう少し抽象的な用語を使うと、</p>
272307
<span class='rusttest'>fn main() {
273308
for var in expression {
274309
code
@@ -278,25 +313,52 @@ <h2 id='for' class='section-header'><a href='#for'>for</a></h2>
278313
<span class='ident'>code</span>
279314
}</pre>
280315

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`. -->
287339

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>
291342

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 -->
295344

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 -->
298346

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() {
300362
for (i,j) in (5..10).enumerate() {
301363
println!(&quot;i = {} and j = {}&quot;, i, j);
302364
}
@@ -305,7 +367,9 @@ <h4 id='on-ranges' class='section-header'><a href='#on-ranges'>On ranges:</a></h
305367
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;i = {} and j = {}&quot;</span>, <span class='ident'>i</span>, <span class='ident'>j</span>);
306368
}</pre>
307369

308-
<p>Outputs:</p>
370+
<!-- Outputs: -->
371+
372+
<p>出力:</p>
309373

310374
<pre><code class="language-text">i = 0 and j = 5
311375
i = 1 and j = 6
@@ -314,9 +378,13 @@ <h4 id='on-ranges' class='section-header'><a href='#on-ranges'>On ranges:</a></h
314378
i = 4 and j = 9
315379
</code></pre>
316380

317-
<p>Don&#39;t forget to add the parentheses around the range.</p>
381+
<!-- Don't forget to add the parentheses around the range. -->
318382

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() {
320388
let lines = &quot;hello\nworld&quot;.lines();
321389
for (linenumber, line) in lines.enumerate() {
322390
println!(&quot;{}: {}&quot;, linenumber, line);
@@ -326,16 +394,22 @@ <h4 id='on-iterators' class='section-header'><a href='#on-iterators'>On iterator
326394
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{}: {}&quot;</span>, <span class='ident'>linenumber</span>, <span class='ident'>line</span>);
327395
}</pre>
328396

329-
<p>Outputs:</p>
397+
<!-- Outputs: -->
398+
399+
<p>出力:</p>
330400

331401
<pre><code class="language-text">0: Content of line one
332402
1: Content of line two
333403
2: Content of line three
334404
3: Content of line four
335405
</code></pre>
336406

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>
339413
<span class='rusttest'>fn main() {
340414
let mut x = 5;
341415
let mut done = false;
@@ -363,11 +437,18 @@ <h2 id='ending-iteration-early' class='section-header'><a href='#ending-iteratio
363437
}
364438
}</pre>
365439

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`. -->
369445

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>
371452
<span class='rusttest'>fn main() {
372453
let mut x = 5;
373454

@@ -389,10 +470,17 @@ <h2 id='ending-iteration-early' class='section-header'><a href='#ending-iteratio
389470
<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>; }
390471
}</pre>
391472

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 -->
393479

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>
396484
<span class='rusttest'>fn main() {
397485
for x in 0..10 {
398486
if x % 2 == 0 { continue; }
@@ -406,27 +494,42 @@ <h2 id='ending-iteration-early' class='section-header'><a href='#ending-iteratio
406494
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{}&quot;</span>, <span class='ident'>x</span>);
407495
}</pre>
408496

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>
417518
<span class='rusttest'>fn main() {
418519
&#39;outer: for x in 0..10 {
419520
&#39;inner: for y in 0..10 {
420-
if x % 2 == 0 { continue &#39;outer; } // continues the loop over x
421-
if y % 2 == 0 { continue &#39;inner; } // continues the loop over y
521+
// if x % 2 == 0 { continue &#39;outer; } // continues the loop over x
522+
if x % 2 == 0 { continue &#39;outer; } // x のループを継続
523+
// if y % 2 == 0 { continue &#39;inner; } // continues the loop over y
524+
if y % 2 == 0 { continue &#39;inner; } // y のループを継続
422525
println!(&quot;x: {}, y: {}&quot;, x, y);
423526
}
424527
}
425528
}</span><pre class='rust rust-example-rendered'>
426529
<span class='lifetime'>&#39;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> {
427530
<span class='lifetime'>&#39;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'>&#39;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'>&#39;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'>&#39;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'>&#39;inner</span>; } <span class='comment'>// y のループを継続</span>
430533
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;x: {}, y: {}&quot;</span>, <span class='ident'>x</span>, <span class='ident'>y</span>);
431534
}
432535
}</pre>

0 commit comments

Comments
 (0)
Please sign in to comment.