1
- % Vectors
2
-
3
- A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard
4
- library type [ ` Vec<T> ` ] [ vec ] . The ` T ` means that we can have vectors
5
- of any type (see the chapter on [ generics] [ generic ] for more).
6
- Vectors always allocate their data on the heap.
7
- You can create them with the ` vec! ` macro:
1
+ % ベクタ
2
+ <!-- % Vectors -->
3
+
4
+ <!-- A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard -->
5
+ <!-- library type [`Vec<T>`][vec]. The `T` means that we can have vectors -->
6
+ <!-- of any type (see the chapter on [generics][generic] for more). -->
7
+ <!-- Vectors always allocate their data on the heap. -->
8
+ <!-- You can create them with the `vec!` macro: -->
9
+ 「ベクタ」は動的な、または「拡張可能な」配列です、標準ライブラリ上で [ ` Vec<T> ` ] [ vec ] として提供されています。
10
+ ` T ` はどんなタイプのベクタをも作成することが可能なことを意味しています。(詳細は[ ジェネリクス] [ generic ] を御覧ください)
11
+ ベクタはデータを常にヒープ上にアロケーションします。
12
+ ベクタは以下のように ` vec! ` マクロを用いて作成できます:
8
13
9
14
``` rust
10
15
let v = vec! [1 , 2 , 3 , 4 , 5 ]; // v: Vec<i32>
11
16
```
12
17
13
- (Notice that unlike the ` println! ` macro we’ve used in the past, we use square
14
- brackets ` [] ` with ` vec! ` macro. Rust allows you to use either in either situation,
15
- this is just convention.)
18
+ <!-- (Notice that unlike the `println!` macro we’ve used in the past, we use square -->
19
+ <!-- brackets `[]` with `vec!` macro. Rust allows you to use either in either situation, -->
20
+ <!-- this is just convention.) -->
21
+ (以前使った` println! ` マクロと異なり、` vec! ` マクロで 角括弧 ` [] ` を利用しました。)
22
+ Rustではどちらの括弧もどちらのシチュエーションでも利用可能であり、解りやすさのためです。
16
23
17
- There’s an alternate form of ` vec! ` for repeating an initial value:
24
+ <!-- There’s an alternate form of `vec!` for repeating an initial value: -->
25
+ ` vec! ` には初期値の繰り返しを表現するための形式があります:
18
26
19
27
``` rust
20
- let v = vec! [0 ; 10 ]; // ten zeroes
28
+ # // let v = vec![0; 10]; // ten zeroes
29
+ let v = vec! [0 ; 10 ]; // 0が10個
21
30
```
22
31
23
- ## Accessing elements
32
+ ## 要素へのアクセス
24
33
25
- To get the value at a particular index in the vector, we use ` [] ` s:
34
+ <!-- To get the value at a particular index in the vector, we use `[]`s: -->
35
+ ベクタ中の特定のインデックスの値にアクセスするには ` [] ` を利用します:
26
36
27
37
``` rust
28
38
let v = vec! [1 , 2 , 3 , 4 , 5 ];
29
39
30
40
println! (" The third element of v is {}" , v [2 ]);
31
41
```
32
42
33
- The indices count from ` 0 ` , so the third element is ` v[2] ` .
43
+ <!-- The indices count from `0`, so the third element is `v[2]`.-->
44
+ インデックスは ` 0 ` から始まります、なので三番目の要素は ` v[2] ` となります。
34
45
35
- It’s also important to note that you must index with the ` usize ` type:
46
+ <!-- It’s also important to note that you must index with the `usize` type: -->
47
+ また、インデックスは ` usize ` 型でなければならない点に注意しましょう:
36
48
37
49
``` ignore
38
50
let v = vec![1, 2, 3, 4, 5];
39
51
40
52
let i: usize = 0;
41
53
let j: i32 = 0;
42
54
43
- // works
55
+ # // // works
56
+ // これは動作します
44
57
v[i];
45
58
46
- // doesn’t
59
+ # // // doesn’t
60
+ // 一方、こちらは動作しません
47
61
v[j];
48
62
```
49
63
50
- Indexing with a non-` usize ` type gives an error that looks like this:
64
+ <!-- Indexing with a non-`usize` type gives an error that looks like this: -->
65
+ ` usize ` 型でないインデックスを用いた場合、以下の様なエラーが発生します:
51
66
52
67
``` text
53
68
error: the trait `core::ops::Index<i32>` is not implemented for the type
@@ -58,13 +73,15 @@ note: the type `collections::vec::Vec<_>` cannot be indexed by `i32`
58
73
error: aborting due to previous error
59
74
```
60
75
61
- There’s a lot of punctuation in that message, but the core of it makes sense:
62
- you cannot index with an ` i32 ` .
76
+ <!-- There’s a lot of punctuation in that message, but the core of it makes sense: -->
77
+ <!-- you cannot index with an `i32`. -->
78
+ エラーメッセージ中には多くの点が含まれていますが、一番大切な部分は ` i32 ` をインデックスとして用いることはできないという点です。
63
79
64
- ## Iterating
80
+ ## イテレーティング
65
81
66
- Once you have a vector, you can iterate through its elements with ` for ` . There
67
- are three versions:
82
+ <!-- Once you have a vector, you can iterate through its elements with `for`. There -->
83
+ <!-- are three versions: -->
84
+ ベクタである値に対して ` for ` を用いて以下の様な3つの方法でイテレートすることができます:
68
85
69
86
``` rust
70
87
let mut v = vec! [1 , 2 , 3 , 4 , 5 ];
@@ -82,8 +99,9 @@ for i in v {
82
99
}
83
100
```
84
101
85
- Vectors have many more useful methods, which you can read about in [ their
86
- API documentation] [ vec ] .
102
+ <!-- Vectors have many more useful methods, which you can read about in [their -->
103
+ <!-- API documentation][vec]. -->
104
+ ベクタにはもっと多くの便利なメソッドが定義されています。それらのメソッドについては [ APIドキュメント] [ vec ] で確認することができます。
87
105
88
106
[ vec ] : ../std/vec/index.html
89
107
[ generic ] : generics.html
0 commit comments