Skip to content

[WIP] 4.7. Vectors (1.9) #168

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

Closed
wants to merge 1 commit into from
Closed
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
101 changes: 88 additions & 13 deletions 1.9/ja/book/vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,36 @@
<!-- of any type (see the chapter on [generics][generic] for more). -->
<!-- Vectors always allocate their data on the heap. -->
<!-- You can create them with the `vec!` macro: -->
「ベクタ」は動的な、または「拡張可能な」配列です、標準ライブラリ上で [`Vec<T>`][vec] として提供されています。
`T` はどんなタイプのベクタをも作成することが可能なことを意味しています。(詳細は[ジェネリクス][generic]を御覧ください)
ベクタはデータを常にヒープ上にアロケーションします
「ベクタ」は動的な、または「拡張可能な」配列で、標準ライブラリ上で [`Vec<T>`][vec] として提供されています。
`T` はどんな型のベクタでも作成できることを意味します。(詳細は[ジェネリクス][generic]をご覧ください)
ベクタはデータを常にヒープ上に割り当てます
ベクタは以下のように `vec!` マクロを用いて作成できます:

```rust
let v = vec![1, 2, 3, 4, 5]; // v: Vec<i32>
```

<!-- (Notice that unlike the `println!` macro we’ve used in the past, we use square -->
<!-- brackets `[]` with `vec!` macro. Rust allows you to use either in either situation, -->
<!-- this is just convention.) -->
(以前使った`println!` マクロと異なり、`vec!` マクロで 角括弧 `[]` を利用しました。)
Rustではどちらの括弧もどちらのシチュエーションでも利用可能であり、解りやすさのためです
<!-- brackets `[]` with `vec!` macro. Rust allows you to use either in either -->
<!-- situation, this is just convention.) -->
(以前使った`println!` マクロと異なり、`vec!` マクロでは角括弧 `[]` を利用しました。
Rustでは状況に応じてどちらの括弧も利用でき、解りやすさのためです)

<!-- There’s an alternate form of `vec!` for repeating an initial value: -->
`vec!` には初期値の繰り返しを表現するための形式があります:
`vec!` には初期値の繰り返しを表現するための別の書き方があります:

```rust
# // let v = vec![0; 10]; // ten zeroes
let v = vec![0; 10]; // 0が10個
```

<!-- Vectors store their contents as contiguous arrays of `T` on the heap. This means -->
<!-- that they must be able to know the size of `T` at compile time (that is, how -->
<!-- many bytes are needed to store a `T`?). The size of some things can't be known -->
<!-- at compile time. For these you'll have to store a pointer to that thing: -->
<!-- thankfully, the [`Box`][box] type works perfectly for this. -->
**TODO**: このパラグラフを翻訳する。

## 要素へのアクセス

<!-- To get the value at a particular index in the vector, we use `[]`s: -->
Expand Down Expand Up @@ -62,11 +69,11 @@ v[j];
```

<!-- Indexing with a non-`usize` type gives an error that looks like this: -->
`usize` 型でないインデックスを用いた場合、以下の様なエラーが発生します:
`usize` 型でないインデックスを用いた場合、以下のようなエラーが発生します:

```text
error: the trait `core::ops::Index<i32>` is not implemented for the type
`collections::vec::Vec<_>` [E0277]
error: the trait bound `collections::vec::Vec<_> : core::ops::Index<i32>`
is not satisfied [E0277]
v[j];
^~~~
note: the type `collections::vec::Vec<_>` cannot be indexed by `i32`
Expand All @@ -75,12 +82,44 @@ error: aborting due to previous error

<!-- There’s a lot of punctuation in that message, but the core of it makes sense: -->
<!-- you cannot index with an `i32`. -->
エラーメッセージ中には多くの点が含まれていますが、一番大切な部分は `i32` をインデックスとして用いることはできないという点です。
エラーメッセージには多くの点が含まれていますが、一番大切なのは `i32` をインデックスとして用いられない点です。

## Out-of-bounds Access

**TODO**: この小節を翻訳する。

<!-- If you try to access an index that doesn’t exist: -->

```ignore
let v = vec![1, 2, 3];
println!("Item 7 is {}", v[7]);
```

<!-- then the current thread will [panic] with a message like this: -->

```text
thread '<main>' panicked at 'index out of bounds: the len is 3 but the index is 7'
```

<!-- If you want to handle out-of-bounds errors without panicking, you can use -->
<!-- methods like [`get`][get] or [`get_mut`][get_mut] that return `None` when -->
<!-- given an invalid index: -->

```rust
let v = vec![1, 2, 3];
match v.get(7) {
Some(x) => println!("Item 7 is {}", x),
None => println!("Sorry, this vector is too short.")
}
```

## イテレーティング

**TODO**: イテレーティングをイテレーションに変えるか検討する。

<!-- Once you have a vector, you can iterate through its elements with `for`. There -->
<!-- are three versions: -->
**TODO**: この訳を修正する。("Once you have a vector", "its elements", "three versions" などが抜けている)
ベクタである値に対して `for` を用いて以下の様な3つの方法でイテレートすることができます:

```rust
Expand All @@ -99,9 +138,45 @@ for i in v {
}
```

<!-- Note: You cannot use the vector again once you have iterated by taking ownership of the vector. -->
<!-- You can iterate the vector multiple times by taking a reference to the vector whilst iterating. -->
<!-- For example, the following code does not compile. -->
**TODO**: Noteを翻訳する。

```rust,ignore
let v = vec![1, 2, 3, 4, 5];

for i in v {
println!("Take ownership of the vector and its element {}", i);
}

for i in v {
println!("Take ownership of the vector and its element {}", i);
}
```

<!-- Whereas the following works perfectly, -->
**TODO**: この文を翻訳する。

```rust
let v = vec![1, 2, 3, 4, 5];

for i in &v {
println!("This is a reference to {}", i);
}

for i in &v {
println!("This is a reference to {}", i);
}
```

<!-- Vectors have many more useful methods, which you can read about in [their -->
<!-- API documentation][vec]. -->
ベクタにはもっと多くの便利なメソッドが定義されています。それらのメソッドについては [APIドキュメント][vec] で確認することができます
ベクタにはさらに多くの便利なメソッドが定義されています。それらのメソッドについては [APIドキュメント][vec] で確認できます

[vec]: ../std/vec/index.html
[box]: ../std/boxed/index.html
[generic]: generics.html
[panic]: concurrency.html#panics
[get]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get
[get_mut]: http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get_mut
115 changes: 0 additions & 115 deletions diff-1.6.0..1.9.0/src/doc/book/vectors.md

This file was deleted.