Skip to content

Commit b140aea

Browse files
committed
以前の翻訳を保ちつつ翻訳をアップデート
1 parent 27676ff commit b140aea

File tree

4 files changed

+59
-98
lines changed

4 files changed

+59
-98
lines changed

listings/ch08-common-collections/listing-08-04/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ fn main() {
33
{
44
let v = vec![1, 2, 3, 4];
55

6-
// do stuff with v
7-
} // <- v goes out of scope and is freed here
6+
// vで作業をする
7+
} // <- vはここでスコープを抜け、解放される
88
// ANCHOR_END: here
99
}

listings/ch08-common-collections/listing-08-05/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ fn main() {
66
println!("The third element is {}", third);
77

88
match v.get(2) {
9+
// "3つ目の要素は{}です"
910
Some(third) => println!("The third element is {}", third),
11+
// "3つ目の要素はありません。"
1012
None => println!("There is no third element."),
1113
}
1214
// ANCHOR_END: here

listings/ch08-common-collections/listing-08-07/output.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
$ cargo run
22
Compiling collections v0.1.0 (file:///projects/collections)
33
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
4+
(エラー: 不変としても借用されているので、`v`を可変で借用できません)
45
--> src/main.rs:6:5
56
|
67
4 | let first = &v[0];
78
| - immutable borrow occurs here
9+
| (不変借用はここで発生しています)
810
5 |
911
6 | v.push(6);
1012
| ^^^^^^^^^ mutable borrow occurs here
13+
| (可変借用はここで発生しています)
1114
7 |
1215
8 | println!("The first element is: {}", first);
1316
| ----- immutable borrow later used here
17+
| (その後、不変借用はここで使われています)
1418

1519
error: aborting due to previous error
1620

src/ch08-01-vectors.md

Lines changed: 51 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<!--
88
The first collection type we’ll look at is `Vec<T>`, also known as a *vector*.
9-
Vectors allow us to store more than one value in a single data structure that
9+
Vectors allow you to store more than one value in a single data structure that
1010
puts all the values next to each other in memory. Vectors can only store values
1111
of the same type. They are useful when you have a list of items, such as the
1212
lines of text in a file or the prices of items in a shopping cart.
@@ -31,7 +31,7 @@ Listing 8-1.
3131
新しい空のベクタを作るには、リスト8-1に示されたように、`Vec::new`関数を呼べばよいです。
3232

3333
```rust
34-
let v: Vec<i32> = Vec::new();
34+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-01/src/main.rs:here}}
3535
```
3636

3737
<!--
@@ -62,19 +62,22 @@ hold elements of the `i32` type.
6262
<!--
6363
In more realistic code, Rust can often infer the type of value you want to
6464
store once you insert values, so you rarely need to do this type annotation.
65-
It's more common to create a `Vec<T>` that has initial values, and Rust
65+
Its more common to create a `Vec<T>` that has initial values, and Rust
6666
provides the `vec!` macro for convenience. The macro will create a new vector
67-
that holds the values we give it. Listing 8-2 creates a new `Vec<i32>` that
68-
holds the values `1`, `2`, and `3`.
67+
that holds the values you give it. Listing 8-2 creates a new `Vec<i32>` that
68+
holds the values `1`, `2`, and `3`. The integer type is `i32` because that’s
69+
the default integer type, as we discussed in the [“Data Types”][data-types]
70+
section of Chapter 3.
6971
-->
7072

7173
より現実的なコードでは、一旦値を挿入したら、コンパイラは保持させたい値の型をしばしば推論できるので、
7274
この型注釈をすることは滅多にありません。初期値のある`Vec<T>`を生成する方が一般的ですし、
7375
Rustには、利便性のために`vec!`というマクロも用意されています。このマクロは、
7476
与えた値を保持する新しいベクタ型を生成します。リスト8-2では、`1``2``3`という値を持つ新しい`Vec<i32>`を生成しています。
77+
整数型を`i32`にしているのは、3章の[「データ型」][data-types]節で学んだようにこれが標準の整数型だからです。
7578

7679
```rust
77-
let v = vec![1, 2, 3];
80+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-02/src/main.rs:here}}
7881
```
7982

8083
<!--
@@ -107,12 +110,7 @@ as shown in Listing 8-3.
107110
ベクタを生成し、それから要素を追加するには、リスト8-3に示したように、`push`メソッドを使用できます。
108111

109112
```rust
110-
let mut v = Vec::new();
111-
112-
v.push(5);
113-
v.push(6);
114-
v.push(7);
115-
v.push(8);
113+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-03/src/main.rs:here}}
116114
```
117115

118116
<!--
@@ -146,28 +144,9 @@ annotated in Listing 8-4.
146144

147145
他のあらゆる`構造体`同様、ベクタもスコープを抜ければ、解放されます。リスト8-4に注釈したようにですね。
148146

149-
<!--
150-
```rust
151-
{
152-
let v = vec![1, 2, 3, 4];
153-
-->
154-
155-
<!--
156-
// do stuff with v
157-
-->
158-
159-
<!--
160-
} // <- v goes out of scope and is freed here
161-
```
162-
-->
163147

164148
```rust
165-
{
166-
let v = vec![1, 2, 3, 4];
167-
168-
// vで作業をする
169-
170-
} // <- vはここでスコープを抜け、解放される
149+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-04/src/main.rs:here}}
171150
```
172151

173152
<!--
@@ -178,7 +157,7 @@ are dropped</span>
178157
<span class="caption">リスト8-4: ベクタとその要素がドロップされる箇所を示す</span>
179158

180159
<!--
181-
When the vector gets dropped, all of its contents will also be dropped, meaning
160+
When the vector gets dropped, all of its contents are also dropped, meaning
182161
those integers it holds will be cleaned up. This may seem like a
183162
straightforward point but can get a bit more complicated when you start to
184163
introduce references to the elements of the vector. Let’s tackle that next!
@@ -213,10 +192,7 @@ indexing syntax or the `get` method.
213192
リスト8-5に示したのは、両メソッドがベクタの値に対して、添字記法と`get`メソッドによりアクセスするところです。
214193

215194
```rust
216-
let v = vec![1, 2, 3, 4, 5];
217-
218-
let third: &i32 = &v[2];
219-
let third: Option<&i32> = v.get(2);
195+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-05/src/main.rs:here}}
220196
```
221197

222198
<!--
@@ -250,11 +226,8 @@ Rustには要素を参照する方法が2通りあるので、ベクタに要素
250226
プログラムの振る舞いを選択できます。例として、ベクタに5つ要素があり、添え字100の要素にアクセスを試みた場合、
251227
プログラムがすることを確認しましょう。リスト8-6に示したようにですね。
252228

253-
```rust,should_panic
254-
let v = vec![1, 2, 3, 4, 5];
255-
256-
let does_not_exist = &v[100];
257-
let does_not_exist = v.get(100);
229+
```rust,should_panic,panics
230+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-06/src/main.rs:here}}
258231
```
259232

260233
<!--
@@ -267,7 +240,7 @@ let does_not_exist = v.get(100);
267240
<!--
268241
When we run this code, the first `[]` method will cause the program to panic
269242
because it references a nonexistent element. This method is best used when you
270-
want your program to crash if there's an attempt to access an element past the
243+
want your program to crash if theres an attempt to access an element past the
271244
end of the vector.
272245
-->
273246

@@ -298,7 +271,7 @@ value. That would be more user-friendly than crashing the program due to a typo!
298271
When the program has a valid reference, the borrow checker enforces the
299272
ownership and borrowing rules (covered in Chapter 4) to ensure this reference
300273
and any other references to the contents of the vector remain valid. Recall the
301-
rule that states we can’t have mutable and immutable references in the same
274+
rule that states you can’t have mutable and immutable references in the same
302275
scope. That rule applies in Listing 8-7, where we hold an immutable reference to
303276
the first element in a vector and try to add an element to the end, which won't
304277
work.
@@ -308,16 +281,11 @@ work.
308281
所有権と借用規則を強制し、ベクタの中身へのこの参照や他のいかなる参照も有効であり続けることを保証してくれます。
309282
同一スコープ上では、可変と不変な参照を同時には存在させられないというルールを思い出してください。
310283
このルールはリスト8-7にも適用され、リスト8-7ではベクタの最初の要素への不変参照を保持し、
311-
終端に要素を追加しようとしていますが、動きません。
312-
313-
```rust,ignore
314-
let mut v = vec![1, 2, 3, 4, 5];
315-
316-
let first = &v[0];
284+
終端に要素を追加しようとしています。
285+
もし、関数内のここ以降で、この要素を参照しようとしている場合、これは動きません。
317286

318-
v.push(6);
319-
320-
println!("The first element is: {}", first);
287+
```rust,ignore,does_not_compile
288+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}}
321289
```
322290

323291
<!--
@@ -333,28 +301,16 @@ Compiling this code will result in this error:
333301

334302
このコードをコンパイルすると、こんなエラーになります:
335303

336-
```text
337-
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
338-
(エラー: 不変としても借用されているので、`v`を可変で借用できません)
339-
|
340-
4 | let first = &v[0];
341-
| - immutable borrow occurs here
342-
| (不変借用はここで発生しています)
343-
5 |
344-
6 | v.push(6);
345-
| ^ mutable borrow occurs here
346-
| (可変借用は、ここで発生しています)
347-
7 | }
348-
| - immutable borrow ends here
349-
| (不変借用はここで終了しています)
304+
```console
305+
{{#include ../listings/ch08-common-collections/listing-08-07/output.txt}}
350306
```
351307

352308
<!--
353309
The code in Listing 8-7 might look like it should work: why should a reference
354310
to the first element care about what changes at the end of the vector? This
355311
error is due to the way vectors work: adding a new element onto the end of the
356312
vector might require allocating new memory and copying the old elements to the
357-
new space if there isn’t enough room to put all the elements next to each
313+
new space, if there isn’t enough room to put all the elements next to each
358314
other where the vector currently is. In that case, the reference to the first
359315
element would be pointing to deallocated memory. The borrowing rules prevent
360316
programs from ending up in that situation.
@@ -368,25 +324,25 @@ programs from ending up in that situation.
368324
そのような場面に陥らないよう回避されるのです。
369325

370326
<!--
371-
> Note: For more on the implementation details of the `Vec<T>` type, see “The
372-
> Rustonomicon” at https://doc.rust-lang.org/stable/nomicon/vec.html.
327+
> Note: For more on the implementation details of the `Vec<T>` type, see [“The
328+
> Rustonomicon”][nomicon].
373329
-->
374330

375-
> 注釈: `Vec<T>`の実装に関する詳細については、[“The Rustonomicon”](https://doc.rust-lang.org/stable/nomicon/vec.html)を参照してください。
331+
> 注釈: `Vec<T>`の実装に関する詳細については、[“The Rustonomicon”][nomicon]を参照してください。
376332
377333
> 訳注: 日本語版のThe Rustonomiconは[こちら][nomicon-ja-vec]です。
378334
379335
[nomicon-ja-vec]: https://doc.rust-jp.rs/rust-nomicon-ja/vec.html
380336

381337
<!--
382-
### Iterating Over the Values in a Vector
338+
### Iterating over the Values in a Vector
383339
-->
384340

385341
### ベクタの値を走査する
386342

387343
<!--
388344
If we want to access each element in a vector in turn, we can iterate through
389-
all of the elements rather than use indexes to access one at a time. Listing
345+
all of the elements rather than use indices to access one at a time. Listing
390346
8-8 shows how to use a `for` loop to get immutable references to each element
391347
in a vector of `i32` values and print them.
392348
-->
@@ -395,10 +351,7 @@ in a vector of `i32` values and print them.
395351
リスト8-8で`for`ループを使い、`i32`のベクタの各要素に対する不変な参照を得て、それらを出力する方法を示しています。
396352

397353
```rust
398-
let v = vec![100, 32, 57];
399-
for i in &v {
400-
println!("{}", i);
401-
}
354+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-08/src/main.rs:here}}
402355
```
403356

404357
<!--
@@ -418,10 +371,7 @@ will add `50` to each element.
418371
リスト8-9の`for`ループでは、各要素に`50`を足しています。
419372

420373
```rust
421-
let mut v = vec![100, 32, 57];
422-
for i in &mut v {
423-
*i += 50;
424-
}
374+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-09/src/main.rs:here}}
425375
```
426376

427377
<!--
@@ -434,11 +384,14 @@ elements in a vector</span>
434384
<!--
435385
To change the value that the mutable reference refers to, we have to use the
436386
dereference operator (`*`) to get to the value in `i` before we can use the
437-
`+=` operator.
387+
`+=` operator. We’ll talk more about the dereference operator in the
388+
[“Following the Pointer to the Value with the Dereference Operator”][deref]
389+
section of Chapter 15.
438390
-->
439391

440392
可変参照が参照している値を変更するには、`+=`演算子を使用する前に、
441393
参照外し演算子(`*`)を使用して`i`の値に辿り着かないといけません。
394+
参照外し演算子については、15章の[「参照外し演算子で値までポインタを追いかける」][deref]節でより詳しく扱います。
442395

443396
<!--
444397
### Using an Enum to Store Multiple Types
@@ -474,17 +427,7 @@ ultimately, holds different types. We’ve demonstrated this in Listing 8-10.
474427
結果的に異なる型を保持できるようになるわけです。リスト8-10でこれを模擬しています。
475428

476429
```rust
477-
enum SpreadsheetCell {
478-
Int(i32),
479-
Float(f64),
480-
Text(String),
481-
}
482-
483-
let row = vec![
484-
SpreadsheetCell::Int(3),
485-
SpreadsheetCell::Text(String::from("blue")),
486-
SpreadsheetCell::Float(10.12),
487-
];
430+
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-10/src/main.rs:here}}
488431
```
489432

490433
<!--
@@ -513,7 +456,7 @@ enumに加えて`match`式を使うことは、第6章で議論した通り、
513456
保証できることを意味します。
514457

515458
<!--
516-
When you're writing a program, if you don’t know the exhaustive set of types
459+
When youre writing a program, if you don’t know the exhaustive set of types
517460
the program will get at runtime to store in a vector, the enum technique won’t
518461
work. Instead, you can use a trait object, which we’ll cover in Chapter 17.
519462
-->
@@ -523,7 +466,7 @@ work. Instead, you can use a trait object, which we’ll cover in Chapter 17.
523466

524467
<!--
525468
Now that we’ve discussed some of the most common ways to use vectors, be sure
526-
to review the API documentation for all the many useful methods defined on
469+
to review [the API documentation][vec-api] for all the many useful methods defined on
527470
`Vec<T>` by the standard library. For example, in addition to `push`, a `pop`
528471
method removes and returns the last element. Let’s move on to the next
529472
collection type: `String`!
@@ -534,5 +477,17 @@ collection type: `String`!
534477
-->
535478

536479
今や、ベクタを使用するべき最も一般的な方法について触れ、議論したので、標準ライブラリで`Vec<T>`に定義されている多くの有益なメソッドについては、
537-
APIドキュメントを確認することを心得てください。例として、`push`に加えて、`pop`メソッドは最後の要素を削除して返します。
480+
[APIドキュメント][vec-api]を確認することを心得てください。例として、`push`に加えて、`pop`メソッドは最後の要素を削除して返します。
538481
次のコレクション型に移りましょう: `String`です!
482+
483+
<!--
484+
[data-types]: ch03-02-data-types.html#data-types
485+
[nomicon]: ../nomicon/vec.html
486+
[vec-api]: ../std/vec/struct.Vec.html
487+
[deref]: ch15-02-deref.html#following-the-pointer-to-the-value-with-the-dereference-operator
488+
-->
489+
490+
[data-types]: ch03-02-data-types.html#データ型
491+
[nomicon]: ../nomicon/vec.html
492+
[vec-api]: ../std/vec/struct.Vec.html
493+
[deref]: ch15-02-deref.html#参照外し演算子で値までポインタを追いかける

0 commit comments

Comments
 (0)