Skip to content

Vectors #63

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

Merged
merged 3 commits into from
Feb 4, 2016
Merged
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
70 changes: 44 additions & 26 deletions 1.6/ja/book/vectors.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
% Vectors

A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard
library type [`Vec<T>`][vec]. The `T` means that we can have vectors
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:
% ベクタ
<!-- % Vectors -->

<!-- A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard -->
<!-- library type [`Vec<T>`][vec]. The `T` means that we can have vectors -->
<!-- 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!` マクロを用いて作成できます:

```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.)
<!-- (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ではどちらの括弧もどちらのシチュエーションでも利用可能であり、解りやすさのためです。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

閉じ括弧が抜けているようです


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

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

## Accessing elements
## 要素へのアクセス

To get the value at a particular index in the vector, we use `[]`s:
<!-- To get the value at a particular index in the vector, we use `[]`s: -->
ベクタ中の特定のインデックスの値にアクセスするには `[]` を利用します:

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

println!("The third element of v is {}", v[2]);
```

The indices count from `0`, so the third element is `v[2]`.
<!-- The indices count from `0`, so the third element is `v[2]`.-->
インデックスは `0` から始まります、なので三番目の要素は `v[2]` となります。

It’s also important to note that you must index with the `usize` type:
<!-- It’s also important to note that you must index with the `usize` type: -->
また、インデックスは `usize` 型でなければならない点に注意しましょう:

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

let i: usize = 0;
let j: i32 = 0;

// works
# // // works
// これは動作します
v[i];

// doesn’t
# // // doesn’t
// 一方、こちらは動作しません
v[j];
```

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

```text
error: the trait `core::ops::Index<i32>` is not implemented for the type
Expand All @@ -58,13 +73,15 @@ note: the type `collections::vec::Vec<_>` cannot be indexed by `i32`
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`.
<!-- There’s a lot of punctuation in that message, but the core of it makes sense: -->
<!-- you cannot index with an `i32`. -->
エラーメッセージ中には多くの点が含まれていますが、一番大切な部分は `i32` をインデックスとして用いることはできないという点です。

## Iterating
## イテレーティング

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

```rust
let mut v = vec![1, 2, 3, 4, 5];
Expand All @@ -82,8 +99,9 @@ for i in v {
}
```

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

[vec]: ../std/vec/index.html
[generic]: generics.html