4
4
< meta charset ="utf-8 ">
5
5
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6
6
< meta name ="generator " content ="rustdoc ">
7
- < title > Vectors </ title >
7
+ < title > ベクタ </ title >
8
8
9
9
< link rel ="stylesheet " type ="text/css " href ="rustbook.css ">
10
10
185
185
< div id ='page '>
186
186
187
187
188
- < h1 class ="title "> Vectors</ h1 >
189
- < p > A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard
190
- library type < a href ="../std/vec/index.html "> < code > Vec<T></ code > </ a > . The < code > T</ code > means that we can have vectors
191
- of any type (see the chapter on < a href ="generics.html "> generics</ a > for more).
192
- Vectors always allocate their data on the heap.
193
- You can create them with the < code > vec!</ code > macro:</ p >
188
+ < h1 class ="title "> ベクタ</ h1 >
189
+ <!-- % Vectors -->
190
+
191
+ <!-- A ‘vector’ is a dynamic or ‘growable’ array, implemented as the standard -->
192
+
193
+ <!-- library type [`Vec<T>`][vec]. The `T` means that we can have vectors -->
194
+
195
+ <!-- of any type (see the chapter on [generics][generic] for more). -->
196
+
197
+ <!-- Vectors always allocate their data on the heap. -->
198
+
199
+ <!-- You can create them with the `vec!` macro: -->
200
+
201
+ < p > 「ベクタ」は動的な、または「拡張可能な」配列です、標準ライブラリ上で < a href ="../std/vec/index.html "> < code > Vec<T></ code > </ a > として提供されています。
202
+ < code > T</ code > はどんなタイプのベクタをも作成することが可能なことを意味しています。(詳細は< a href ="generics.html "> ジェネリクス</ a > を御覧ください)
203
+ ベクタはデータを常にヒープ上にアロケーションします。
204
+ ベクタは以下のように < code > vec!</ code > マクロを用いて作成できます:</ p >
194
205
< span class ='rusttest '> fn main() {
195
206
let v = vec![1, 2, 3, 4, 5]; // v: Vec<i32>
196
207
}</ span > < pre class ='rust rust-example-rendered '>
197
208
< span class ='kw '> let</ span > < span class ='ident '> v</ span > < span class ='op '> =</ span > < span class ='macro '> vec</ span > < span class ='macro '> !</ span > [< span class ='number '> 1</ span > , < span class ='number '> 2</ span > , < span class ='number '> 3</ span > , < span class ='number '> 4</ span > , < span class ='number '> 5</ span > ]; < span class ='comment '> // v: Vec<i32></ span > </ pre >
198
209
199
- < p > (Notice that unlike the < code > println!</ code > macro we’ve used in the past, we use square
200
- brackets < code > []</ code > with < code > vec!</ code > macro. Rust allows you to use either in either situation,
201
- this is just convention.)</ p >
210
+ <!-- (Notice that unlike the `println!` macro we’ve used in the past, we use square -->
211
+
212
+ <!-- brackets `[]` with `vec!` macro. Rust allows you to use either in either situation, -->
213
+
214
+ <!-- this is just convention.) -->
215
+
216
+ < p > (以前使った< code > println!</ code > マクロと異なり、< code > vec!</ code > マクロで 角括弧 < code > []</ code > を利用しました。)
217
+ Rustではどちらの括弧もどちらのシチュエーションでも利用可能であり、解りやすさのためです。</ p >
218
+
219
+ <!-- There’s an alternate form of `vec!` for repeating an initial value: -->
202
220
203
- < p > There’s an alternate form of < code > vec!</ code > for repeating an initial value :</ p >
221
+ < p > < code > vec!</ code > には初期値の繰り返しを表現するための形式があります :</ p >
204
222
< span class ='rusttest '> fn main() {
205
- let v = vec![0; 10]; // ten zeroes
223
+ // let v = vec![0; 10]; // ten zeroes
224
+ let v = vec![0; 10]; // 0が10個
206
225
}</ span > < pre class ='rust rust-example-rendered '>
207
- < span class ='kw '> let</ span > < span class ='ident '> v</ span > < span class ='op '> =</ span > < span class ='macro '> vec</ span > < span class ='macro '> !</ span > [< span class ='number '> 0</ span > ; < span class ='number '> 10</ span > ]; < span class ='comment '> // ten zeroes </ span > </ pre >
226
+ < span class ='kw '> let</ span > < span class ='ident '> v</ span > < span class ='op '> =</ span > < span class ='macro '> vec</ span > < span class ='macro '> !</ span > [< span class ='number '> 0</ span > ; < span class ='number '> 10</ span > ]; < span class ='comment '> // 0が10個 </ span > </ pre >
208
227
209
- < h2 id ='accessing-elements ' class ='section-header '> < a href ='#accessing-elements '> Accessing elements</ a > </ h2 >
210
- < p > To get the value at a particular index in the vector, we use < code > []</ code > s:</ p >
228
+ < h2 id ='要素へのアクセス ' class ='section-header '> < a href ='#要素へのアクセス '> 要素へのアクセス</ a > </ h2 >
229
+ <!-- To get the value at a particular index in the vector, we use `[]`s: -->
230
+
231
+ < p > ベクタ中の特定のインデックスの値にアクセスするには < code > []</ code > を利用します:</ p >
211
232
< span class ='rusttest '> fn main() {
212
233
let v = vec![1, 2, 3, 4, 5];
213
234
@@ -217,33 +238,41 @@ <h2 id='accessing-elements' class='section-header'><a href='#accessing-elements'
217
238
218
239
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "The third element of v is {}"</ span > , < span class ='ident '> v</ span > [< span class ='number '> 2</ span > ]);</ pre >
219
240
220
- < p > The indices count from < code > 0</ code > , so the third element is < code > v[2]</ code > .</ p >
241
+ <!-- The indices count from `0`, so the third element is `v[2]`.-->
242
+
243
+ < p > インデックスは < code > 0</ code > から始まります、なので三番目の要素は < code > v[2]</ code > となります。</ p >
244
+
245
+ <!-- It’s also important to note that you must index with the `usize` type: -->
221
246
222
- < p > It’s also important to note that you must index with the < code > usize</ code > type :</ p >
247
+ < p > また、インデックスは < code > usize</ code > 型でなければならない点に注意しましょう :</ p >
223
248
< span class ='rusttest '> fn main() {
224
249
let v = vec![1, 2, 3, 4, 5];
225
250
226
251
let i: usize = 0;
227
252
let j: i32 = 0;
228
253
229
- // works
254
+ // // works
255
+ // これは動作します
230
256
v[i];
231
257
232
- // doesn’t
258
+ // // doesn’t
259
+ // 一方、こちらは動作しません
233
260
v[j];
234
261
}</ span > < pre class ='rust rust-example-rendered '>
235
262
< span class ='kw '> let</ span > < span class ='ident '> v</ span > < span class ='op '> =</ span > < span class ='macro '> vec</ span > < span class ='macro '> !</ span > [< span class ='number '> 1</ span > , < span class ='number '> 2</ span > , < span class ='number '> 3</ span > , < span class ='number '> 4</ span > , < span class ='number '> 5</ span > ];
236
263
237
264
< span class ='kw '> let</ span > < span class ='ident '> i</ span > : < span class ='ident '> usize</ span > < span class ='op '> =</ span > < span class ='number '> 0</ span > ;
238
265
< span class ='kw '> let</ span > < span class ='ident '> j</ span > : < span class ='ident '> i32</ span > < span class ='op '> =</ span > < span class ='number '> 0</ span > ;
239
266
240
- < span class ='comment '> // works </ span >
267
+ < span class ='comment '> // これは動作します </ span >
241
268
< span class ='ident '> v</ span > [< span class ='ident '> i</ span > ];
242
269
243
- < span class ='comment '> // doesn’t </ span >
270
+ < span class ='comment '> // 一方、こちらは動作しません </ span >
244
271
< span class ='ident '> v</ span > [< span class ='ident '> j</ span > ];</ pre >
245
272
246
- < p > Indexing with a non-< code > usize</ code > type gives an error that looks like this:</ p >
273
+ <!-- Indexing with a non-`usize` type gives an error that looks like this: -->
274
+
275
+ < p > < code > usize</ code > 型でないインデックスを用いた場合、以下の様なエラーが発生します:</ p >
247
276
248
277
< pre > < code class ="language-text "> error: the trait `core::ops::Index<i32>` is not implemented for the type
249
278
`collections::vec::Vec<_>` [E0277]
@@ -253,12 +282,18 @@ <h2 id='accessing-elements' class='section-header'><a href='#accessing-elements'
253
282
error: aborting due to previous error
254
283
</ code > </ pre >
255
284
256
- < p > There’s a lot of punctuation in that message, but the core of it makes sense:
257
- you cannot index with an < code > i32</ code > .</ p >
285
+ <!-- There’s a lot of punctuation in that message, but the core of it makes sense: -->
286
+
287
+ <!-- you cannot index with an `i32`. -->
258
288
259
- < h2 id ='iterating ' class ='section-header '> < a href ='#iterating '> Iterating</ a > </ h2 >
260
- < p > Once you have a vector, you can iterate through its elements with < code > for</ code > . There
261
- are three versions:</ p >
289
+ < p > エラーメッセージ中には多くの点が含まれていますが、一番大切な部分は < code > i32</ code > をインデックスとして用いることはできないという点です。</ p >
290
+
291
+ < h2 id ='イテレーティング ' class ='section-header '> < a href ='#イテレーティング '> イテレーティング</ a > </ h2 >
292
+ <!-- Once you have a vector, you can iterate through its elements with `for`. There -->
293
+
294
+ <!-- are three versions: -->
295
+
296
+ < p > ベクタである値に対して < code > for</ code > を用いて以下の様な3つの方法でイテレートすることができます:</ p >
262
297
< span class ='rusttest '> fn main() {
263
298
let mut v = vec![1, 2, 3, 4, 5];
264
299
@@ -288,8 +323,11 @@ <h2 id='iterating' class='section-header'><a href='#iterating'>Iterating</a></h2
288
323
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "Take ownership of the vector and its element {}"</ span > , < span class ='ident '> i</ span > );
289
324
}</ pre >
290
325
291
- < p > Vectors have many more useful methods, which you can read about in < a href ="../std/vec/index.html "> their
292
- API documentation</ a > .</ p >
326
+ <!-- Vectors have many more useful methods, which you can read about in [their -->
327
+
328
+ <!-- API documentation][vec]. -->
329
+
330
+ < p > ベクタにはもっと多くの便利なメソッドが定義されています。それらのメソッドについては < a href ="../std/vec/index.html "> APIドキュメント</ a > で確認することができます。</ p >
293
331
294
332
< script type ="text/javascript ">
295
333
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments