Skip to content

Commit ce0c738

Browse files
committed
Merge pull request #43 from Nnwww/5_5_Iterators_fix_vector
[修正] 5.5. iterators
2 parents 4f9100a + b6c7165 commit ce0c738

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

1.6/ja/book/iterators.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ to accomplish various tasks. But first, a few notes about limitations of ranges.
5757
following Rust anti-pattern: using ranges to emulate a C-style `for` loop. Let’s
5858
suppose you needed to iterate over the contents of a vector. You may be tempted
5959
to write this: -->
60-
レンジはとても素朴な機能ですから、度々別のより良い手段を用いることもあります。ここであるRustのアンチパターンについて考えてみましょう。それはレンジをC言語ライクな `for` ループの再現に用いることです。例えばvectorの中身をイテレートする必要があったとしましょう。あなたはこう書きたくなるかもしれません。
60+
レンジはとても素朴な機能ですから、度々別のより良い手段を用いることもあります。ここであるRustのアンチパターンについて考えてみましょう。それはレンジをC言語ライクな `for` ループの再現に用いることです。例えばベクタの中身をイテレートする必要があったとしましょう。あなたはこう書きたくなるかもしれません。
6161

6262
```rust
6363
let nums = vec![1, 2, 3];
@@ -69,7 +69,7 @@ for i in 0..nums.len() {
6969

7070
<!-- This is strictly worse than using an actual iterator. You can iterate over vectors
7171
directly, so write this: -->
72-
これは実際のイテレータの使い方からすれば全く正しくありません。あなたはvectorを直接反復処理できるのですから、こう書くべきです。
72+
これは実際のイテレータの使い方からすれば全く正しくありません。あなたはベクタを直接反復処理できるのですから、こう書くべきです。
7373

7474
```rust
7575
let nums = vec![1, 2, 3];
@@ -87,7 +87,7 @@ the first version will have extra bounds checking because it used indexing,
8787
with the iterator, there's no bounds checking in the second example. This is
8888
very common with iterators: we can ignore unnecessary bounds checks, but still
8989
know that we're safe. -->
90-
これには2つの理由があります。第一に、この方が書き手の意図をはっきり表せています。私たちはvectorのインデックスを作成してからその要素を繰り返し参照したいのではなく、vector自体を反復処理したいのです。第二に、このバージョンのほうがより効率的です。1つ目の例では `num[i]` というようにインデックスを介し参照しているため、余計な境界チェックが発生します。しかし、イテレータが順番にvectorの各要素への参照を生成していくため、2つ目の例では境界チェックが発生しません。これはイテレータにとってごく一般的な性質です。不要な境界チェックを省くことができ、それでもなお安全なままなのです。
90+
これには2つの理由があります。第一に、この方が書き手の意図をはっきり表せています。私たちはベクタのインデックスを作成してからその要素を繰り返し参照したいのではなく、ベクタ自体を反復処理したいのです。第二に、このバージョンのほうがより効率的です。1つ目の例では `num[i]` というようにインデックスを介し参照しているため、余計な境界チェックが発生します。しかし、イテレータが順番にベクタの各要素への参照を生成していくため、2つ目の例では境界チェックが発生しません。これはイテレータにとってごく一般的な性質です。不要な境界チェックを省くことができ、それでもなお安全なままなのです。
9191

9292
<!-- There's another detail here that's not 100% clear because of how `println!`
9393
works. `num` is actually of type `&i32`. That is, it's a reference to an `i32`,
@@ -157,15 +157,15 @@ let one_to_one_hundred = (1..101).collect::<Vec<i32>>();
157157
and so we tell it that we want a vector of integers. You don't always
158158
need to use the whole type, though. Using a `_` will let you provide
159159
a partial hint: -->
160-
もしあなたが覚えているなら、 `::<>` 構文で型ヒント(type hint)を与え、整数型のvectorが欲しいと伝えることができます。しかし、常に型をまるごとを書く必要はありません`_` を用いることで部分的に推論してくれます。
160+
もしあなたが覚えているなら、 `::<>` 構文で型ヒント(type hint)を与え、整数型のベクタが欲しいと伝えることができます。かといって常に型をまるごとを書く必要はありません`_` を用いることで部分的に推論してくれます。
161161

162162
```rust
163163
let one_to_one_hundred = (1..101).collect::<Vec<_>>();
164164
```
165165

166166
<!-- This says "Collect into a `Vec<T>`, please, but infer what the `T` is for me."
167167
`_` is sometimes called a "type placeholder" for this reason. -->
168-
これは「値を `Vec<T>` の中に集めて下さい、しかし `T` を私のために推論して下さい」という意味です。このため `_` は「型プレースホルダ」(type placeholder)と呼ばれることもあります。
168+
これは「値を `Vec<T>` の中に集めて下さい、しかし `T` は私のために推論して下さい」という意味です。このため `_` は「型プレースホルダ」(type placeholder)と呼ばれることもあります。
169169

170170
<!-- `collect()` is the most common consumer, but there are others too. `find()`
171171
is one: -->
@@ -230,12 +230,12 @@ the second element of the array, `2`. `1 + 2 = 3`, and so that becomes
230230
the value of the accumulator for the last iteration. On that iteration,
231231
`x` is the last element, `3`, and `3 + 3 = 6`, which is our final
232232
result for our sum. `1 + 2 + 3 = 6`, and that's the result we got. -->
233-
というわけで、 `0` がbaseで、 `sum` がaccumulatorで、xがelementです。1度目の反復では、私たちはsumに0をセットし、 `nums` の1つ目の要素 `1``x` になります。その後 `sum``x` を足し、 `0 + 1 = 1` を計算します。2度目の反復では前回の `sum` がaccumulatorになり、elementは値の列の2番目の要素 `2` になります。 `1 + 2 = 3` の結果は最後の反復処理におけるaccumulatorの値になります。最後の反復処理において、 `x` は最後の要素 `3` であり、 `3 + 3 = 6` が最終的な結果となります。 `1 + 2 + 3 = 6` が、得られる結果となります
233+
というわけで、 `0` がbaseで、 `sum` がaccumulatorで、xがelementです。1度目の反復では、私たちはsumに0をセットし、 `nums` の1つ目の要素 `1``x` になります。その後 `sum``x` を足し、 `0 + 1 = 1` を計算します。2度目の反復では前回の `sum` がaccumulatorになり、elementは値の列の2番目の要素 `2` になります。 `1 + 2 = 3` の結果は最後の反復処理におけるaccumulatorの値になります。最後の反復処理において、 `x` は最後の要素 `3` であり、 `3 + 3 = 6` が最終的な結果となります。 `1 + 2 + 3 = 6` 、これが得られる結果となります
234234

235235
<!-- Whew. `fold` can be a bit strange the first few times you see it, but once it
236236
clicks, you can use it all over the place. Any time you have a list of things,
237237
and you want a single result, `fold` is appropriate. -->
238-
ふぅ、ようやく説明し終わりました。 `fold` は初めのうちこそ少し奇妙に見えるかもしれませんが、一度理解すればあらゆる場面で使えるでしょう。何かのリストを持っていて、そこから1つの結果を求めたいときならいつでも`fold` は適切な処理です。
238+
ふぅ、ようやく説明し終わりました。 `fold` は初めのうちこそ少し奇妙に見えるかもしれませんが、一度理解すればあらゆる場面で使えるでしょう。何らかのリストを持っていて、そこから1つの結果を求めたい時ならいつでも`fold` は適切な処理です。
239239

240240
<!-- Consumers are important due to one additional property of iterators we haven't
241241
talked about yet: laziness. Let's talk some more about iterators, and you'll
@@ -267,12 +267,12 @@ let nums = (1..100).collect::<Vec<i32>>();
267267

268268
<!-- Now, `collect()` will require that the range gives it some numbers, and so
269269
it will do the work of generating the sequence. -->
270-
今、 `collect()` は幾つかの数値を渡してくれるレンジを要求し、シーケンスを生成する作業を行います。
270+
`collect()` は幾つかの数値を渡してくれるレンジを要求し、シーケンスを生成する作業を行います。
271271

272272
<!-- Ranges are one of two basic iterators that you'll see. The other is `iter()`.
273273
`iter()` can turn a vector into a simple iterator that gives you each element
274274
in turn: -->
275-
レンジは基本的な2つのイテレータのうちの1つです。もう片方は `iter()` です。 `iter()` はvectorを順番に各要素を渡してくれる単純なイテレータに変換できます
275+
レンジは基本的な2つのイテレータのうちの1つです。もう片方は `iter()` です。 `iter()` はベクタを順に各要素を渡すシンプルなイテレータへ変換できます
276276

277277
```rust
278278
let nums = vec![1, 2, 3];
@@ -385,7 +385,7 @@ a few times, and then consume the result. Check it out: -->
385385
```
386386

387387
<!-- This will give you a vector containing `6`, `12`, `18`, `24`, and `30`. -->
388-
これは `6, 12, 18, 24,` 、そして `30` が入ったvectorがあなたに返されます
388+
これは `6, 12, 18, 24,` 、そして `30` が入ったベクタがあなたに渡されます
389389

390390
<!-- This is just a small taste of what iterators, iterator adaptors, and consumers
391391
can help you with. There are a number of really useful iterators, and you can

TranslationTable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@
6767
| tick | クオート
6868
| trait | トレイト
6969
| unsized type | サイズ不定型
70-
| vector | vector
70+
| vector | ベクタ
7171
| wildcard | ワイルドカード

0 commit comments

Comments
 (0)