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 > Generics </ 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 "> Generics</ h1 >
189
- < p > Sometimes, when writing a function or data type, we may want it to work for
188
+ < h1 class ="title "> ジェネリクス</ h1 >
189
+ <!-- % Generics -->
190
+
191
+ <!-- Sometimes, when writing a function or data type, we may want it to work for
190
192
multiple types of arguments. In Rust, we can do this with generics.
191
193
Generics are called ‘parametric polymorphism’ in type theory,
192
194
which means that they are types or functions that have multiple forms (‘poly’
193
- is multiple, ‘morph’ is form) over a given parameter (‘parametric’).</ p >
195
+ is multiple, ‘morph’ is form) over a given parameter (‘parametric’). -->
196
+
197
+ < p > 時々、関数やデータ型を書いていると、引数が複数の型に対応したものが欲しくなることもあります。Rustでは、ジェネリクスを用いてこれを実現しています。ジェネリクスは型理論において「パラメトリック多相」(parametric polymorphism)と呼ばれ、与えられたパラメータにより(「parametric」)型もしくは関数が多数の様相(「poly」は多様、「morph」は様相を意味します)(訳注: ここで「様相」は型を指します)を持つことを意味しています。</ p >
198
+
199
+ <!-- Anyway, enough type theory, let’s check out some generic code. Rust’s
200
+ standard library provides a type, `Option<T>`, that’s generic: -->
194
201
195
- < p > Anyway, enough type theory, let’s check out some generic code. Rust’s
196
- standard library provides a type, < code > Option<T></ code > , that’s generic:</ p >
202
+ < p > さて、型理論はもう十分です。続いてジェネリックなコードを幾つか見ていきましょう。Rustが標準ライブラリで提供している型 < code > Option<T></ code > はジェネリックです。</ p >
197
203
< span class ='rusttest '> fn main() {
198
204
enum Option<T> {
199
205
Some(T),
@@ -205,20 +211,24 @@ <h1 class="title">Generics</h1>
205
211
< span class ='prelude-val '> None</ span > ,
206
212
}</ pre >
207
213
208
- < p > The < code > <T> </ code > part, which you’ve seen a few times before, indicates that this is
209
- a generic data type. Inside the declaration of our < code > enum</ code > , wherever we see a < code > T </ code > ,
214
+ <!-- The `<T>` part, which you’ve seen a few times before, indicates that this is
215
+ a generic data type. Inside the declaration of our ` enum` , wherever we see a `T` ,
210
216
we substitute that type for the same type used in the generic. Here’s an
211
- example of using < code > Option<T></ code > , with some extra type annotations:</ p >
217
+ example of using `Option<T>`, with some extra type annotations: -->
218
+
219
+ < p > < code > <T></ code > の部分は、前に少し見たことがあると思いますが、これがジェネリックなデータ型であることを示しています。 < code > enum</ code > の宣言内であれば、どこでも < code > T</ code > を使うことができ、宣言内に登場する同じ型をジェネリック内で < code > T</ code > 型に置き換えています。型注釈を用いた< code > Option<T></ code > の使用例が以下になります。</ p >
212
220
< span class ='rusttest '> fn main() {
213
221
let x: Option<i32> = Some(5);
214
222
}</ span > < pre class ='rust rust-example-rendered '>
215
223
< span class ='kw '> let</ span > < span class ='ident '> x</ span > : < span class ='prelude-ty '> Option</ span > < span class ='op '> <</ span > < span class ='ident '> i32</ span > < span class ='op '> ></ span > < span class ='op '> =</ span > < span class ='prelude-val '> Some</ span > (< span class ='number '> 5</ span > );</ pre >
216
224
217
- < p > In the type declaration, we say < code > Option<i32></ code > . Note how similar this looks to
218
- < code > Option<T></ code > . So, in this particular < code > Option</ code > , < code > T</ code > has the value of < code > i32</ code > . On
219
- the right-hand side of the binding, we make a < code > Some(T)</ code > , where < code > T</ code > is < code > 5</ code > .
220
- Since that’s an < code > i32</ code > , the two sides match, and Rust is happy. If they didn’t
221
- match, we’d get an error:</ p >
225
+ <!-- In the type declaration, we say `Option<i32>`. Note how similar this looks to
226
+ `Option<T>`. So, in this particular `Option`, `T` has the value of `i32`. On
227
+ the right-hand side of the binding, we make a `Some(T)`, where `T` is `5`.
228
+ Since that’s an `i32`, the two sides match, and Rust is happy. If they didn’t
229
+ match, we’d get an error: -->
230
+
231
+ < p > この型宣言では < code > Option<i32></ code > と書かれています。 < code > Option<T></ code > の違いに注目して下さい。そう、上記の < code > Option</ code > では < code > T</ code > の値は < code > i32</ code > です。この束縛の右辺の < code > Some(T)</ code > では、 < code > T</ code > は < code > 5</ code > となります。それが < code > i32</ code > なので、両辺の型が一致するため、Rustは満足します。型が不一致であれば、以下のようなエラーが発生します。</ p >
222
232
< span class ='rusttest '> fn main() {
223
233
let x: Option<f64> = Some(5);
224
234
// error: mismatched types: expected `core::option::Option<f64>`,
@@ -228,18 +238,24 @@ <h1 class="title">Generics</h1>
228
238
< span class ='comment '> // error: mismatched types: expected `core::option::Option<f64>`,</ span >
229
239
< span class ='comment '> // found `core::option::Option<_>` (expected f64 but found integral variable)</ span > </ pre >
230
240
231
- < p > That doesn’t mean we can’t make < code > Option<T></ code > s that hold an < code > f64</ code > ! They just have
232
- to match up:</ p >
241
+ <!-- That doesn’t mean we can’t make `Option<T>`s that hold an `f64`! They just have
242
+ to match up: -->
243
+
244
+ < p > これは < code > f64</ code > を保持する < code > Option<T></ code > が作れないという意味ではありませんからね!リテラルと宣言の型をぴったり合わせなければなりません。</ p >
233
245
< span class ='rusttest '> fn main() {
234
246
let x: Option<i32> = Some(5);
235
247
let y: Option<f64> = Some(5.0f64);
236
248
}</ span > < pre class ='rust rust-example-rendered '>
237
249
< span class ='kw '> let</ span > < span class ='ident '> x</ span > : < span class ='prelude-ty '> Option</ span > < span class ='op '> <</ span > < span class ='ident '> i32</ span > < span class ='op '> ></ span > < span class ='op '> =</ span > < span class ='prelude-val '> Some</ span > (< span class ='number '> 5</ span > );
238
250
< span class ='kw '> let</ span > < span class ='ident '> y</ span > : < span class ='prelude-ty '> Option</ span > < span class ='op '> <</ span > < span class ='ident '> f64</ span > < span class ='op '> ></ span > < span class ='op '> =</ span > < span class ='prelude-val '> Some</ span > (< span class ='number '> 5.0f64</ span > );</ pre >
239
251
240
- < p > This is just fine. One definition, multiple uses.</ p >
252
+ <!-- This is just fine. One definition, multiple uses. -- >
241
253
242
- < p > Generics don’t have to only be generic over one type. Consider another type from Rust’s standard library that’s similar, < code > Result<T, E></ code > :</ p >
254
+ < p > これだけで結構です。1つの定義で、多くの用途が得られます。</ p >
255
+
256
+ <!-- Generics don’t have to only be generic over one type. Consider another type from Rust’s standard library that’s similar, `Result<T, E>`: -->
257
+
258
+ < p > ジェネリクスにおいてジェネリックな型は1つまで、といった制限はありません。Rustの標準ライブラリに入っている類似の型 < code > Result<T, E></ code > について考えてみます。</ p >
243
259
< span class ='rusttest '> fn main() {
244
260
enum Result<T, E> {
245
261
Ok(T),
@@ -251,8 +267,10 @@ <h1 class="title">Generics</h1>
251
267
< span class ='prelude-val '> Err</ span > (< span class ='ident '> E</ span > ),
252
268
}</ pre >
253
269
254
- < p > This type is generic over < em > two</ em > types: < code > T</ code > and < code > E</ code > . By the way, the capital letters
255
- can be any letter you’d like. We could define < code > Result<T, E></ code > as:</ p >
270
+ <!-- This type is generic over _two_ types: `T` and `E`. By the way, the capital letters
271
+ can be any letter you’d like. We could define `Result<T, E>` as: -->
272
+
273
+ < p > この型では < code > T</ code > と < code > E</ code > の < em > 2つ</ em > がジェネリックです。ちなみに、大文字の部分はあなたの好きな文字で構いません。もしあなたが望むなら < code > Result<T, E></ code > を、</ p >
256
274
< span class ='rusttest '> fn main() {
257
275
enum Result<A, Z> {
258
276
Ok(A),
@@ -264,27 +282,40 @@ <h1 class="title">Generics</h1>
264
282
< span class ='prelude-val '> Err</ span > (< span class ='ident '> Z</ span > ),
265
283
}</ pre >
266
284
267
- < p > if we wanted to. Convention says that the first generic parameter should be
268
- < code > T</ code > , for ‘type’, and that we use < code > E</ code > for ‘error’. Rust doesn’t care, however.</ p >
285
+ <!-- if we wanted to. Convention says that the first generic parameter should be
286
+ `T`, for ‘type’, and that we use `E` for ‘error’. Rust doesn’t care, however. -->
287
+
288
+ < p > のように定義できます。慣習としては、「Type」から第1ジェネリックパラメータは < code > T</ code > であるべきですし、「Error」から < code > E</ code > を用いるのですが、Rustは気にしません。</ p >
289
+
290
+ <!-- The `Result<T, E>` type is intended to be used to return the result of a
291
+ computation, and to have the ability to return an error if it didn’t work out. -->
269
292
270
- < p > The < code > Result<T, E></ code > type is intended to be used to return the result of a
271
- computation, and to have the ability to return an error if it didn’t work out.</ p >
293
+ < p > < code > Result<T, E></ code > 型は計算の結果を返すために使われることが想定されており、正常に動作しなかった場合にエラーの値を返す機能を持っています。</ p >
272
294
273
- < h2 id ='generic-functions ' class ='section-header '> < a href ='#generic-functions '> Generic functions</ a > </ h2 >
274
- < p > We can write functions that take generic types with a similar syntax:</ p >
295
+ <!-- ## Generic functions -->
296
+
297
+ < h2 id ='ジェネリック関数 ' class ='section-header '> < a href ='#ジェネリック関数 '> ジェネリック関数</ a > </ h2 >
298
+ <!-- We can write functions that take generic types with a similar syntax: -->
299
+
300
+ < p > 似た構文でジェネリックな型を取る関数を記述できます。</ p >
275
301
< span class ='rusttest '> fn main() {
276
302
fn takes_anything<T>(x: T) {
277
- // do something with x
303
+ // do something with x
304
+ // xで何か行う
278
305
}
279
306
}</ span > < pre class ='rust rust-example-rendered '>
280
307
< span class ='kw '> fn</ span > < span class ='ident '> takes_anything</ span > < span class ='op '> <</ span > < span class ='ident '> T</ span > < span class ='op '> ></ span > (< span class ='ident '> x</ span > : < span class ='ident '> T</ span > ) {
281
- < span class ='comment '> // do something with x </ span >
308
+ < span class ='comment '> // xで何か行う </ span >
282
309
}</ pre >
283
310
284
- < p > The syntax has two parts: the < code > <T></ code > says “this function is generic over one
285
- type, < code > T</ code > ”, and the < code > x: T</ code > says “x has the type < code > T</ code > .”</ p >
311
+ <!-- The syntax has two parts: the `<T>` says “this function is generic over one
312
+ type, `T`”, and the `x: T` says “x has the type `T`.” -->
313
+
314
+ < p > 構文は2つのパーツから成ります。 < code > <T></ code > は「この関数は1つの型、 < code > T</ code > に対してジェネリックである」ということであり、 < code > x: T</ code > は「xは < code > T</ code > 型である」という意味です。</ p >
286
315
287
- < p > Multiple arguments can have the same generic type:</ p >
316
+ <!-- Multiple arguments can have the same generic type: -->
317
+
318
+ < p > 複数の引数が同じジェネリックな型を持つこともできます。</ p >
288
319
< span class ='rusttest '> fn main() {
289
320
fn takes_two_of_the_same_things<T>(x: T, y: T) {
290
321
// ...
@@ -294,7 +325,9 @@ <h2 id='generic-functions' class='section-header'><a href='#generic-functions'>G
294
325
< span class ='comment '> // ...</ span >
295
326
}</ pre >
296
327
297
- < p > We could write a version that takes multiple types:</ p >
328
+ <!-- We could write a version that takes multiple types: -->
329
+
330
+ < p > 複数の型を取るバージョンを記述することも可能です。</ p >
298
331
< span class ='rusttest '> fn main() {
299
332
fn takes_two_things<T, U>(x: T, y: U) {
300
333
// ...
@@ -304,8 +337,12 @@ <h2 id='generic-functions' class='section-header'><a href='#generic-functions'>G
304
337
< span class ='comment '> // ...</ span >
305
338
}</ pre >
306
339
307
- < h2 id ='generic-structs ' class ='section-header '> < a href ='#generic-structs '> Generic structs</ a > </ h2 >
308
- < p > You can store a generic type in a < code > struct</ code > as well:</ p >
340
+ <!-- ## Generic structs -->
341
+
342
+ < h2 id ='ジェネリック構造体 ' class ='section-header '> < a href ='#ジェネリック構造体 '> ジェネリック構造体</ a > </ h2 >
343
+ <!-- You can store a generic type in a `struct` as well: -->
344
+
345
+ < p > また、 < code > struct</ code > 内にジェネリックな型の値を保存することもできます。</ p >
309
346
< span class ='rusttest '> fn main() {
310
347
struct Point<T> {
311
348
x: T,
@@ -323,11 +360,15 @@ <h2 id='generic-structs' class='section-header'><a href='#generic-structs'>Gener
323
360
< span class ='kw '> let</ span > < span class ='ident '> int_origin</ span > < span class ='op '> =</ span > < span class ='ident '> Point</ span > { < span class ='ident '> x</ span > : < span class ='number '> 0</ span > , < span class ='ident '> y</ span > : < span class ='number '> 0</ span > };
324
361
< span class ='kw '> let</ span > < span class ='ident '> float_origin</ span > < span class ='op '> =</ span > < span class ='ident '> Point</ span > { < span class ='ident '> x</ span > : < span class ='number '> 0.0</ span > , < span class ='ident '> y</ span > : < span class ='number '> 0.0</ span > };</ pre >
325
362
326
- < p > Similar to functions, the < code > <T></ code > is where we declare the generic parameters,
327
- and we then use < code > x: T</ code > in the type declaration, too.</ p >
363
+ <!-- Similar to functions, the `<T>` is where we declare the generic parameters,
364
+ and we then use `x: T` in the type declaration, too. -->
365
+
366
+ < p > 関数と同様に、 < code > <T></ code > がジェネリックパラメータを宣言する場所であり、型宣言において < code > x: T</ code > を使うのも同じです。</ p >
328
367
329
- < p > When you want to add an implementation for the generic < code > struct</ code > , you just
330
- declare the type parameter after the < code > impl</ code > :</ p >
368
+ <!-- When you want to add an implementation for the generic `struct`, you just
369
+ declare the type parameter after the `impl`: -->
370
+
371
+ < p > ジェネリックな < code > struct</ code > に実装を追加したい場合、 < code > impl</ code > の後に型パラメータを宣言するだけです。</ p >
331
372
< span class ='rusttest '> fn main() {
332
373
struct Point<T> {
333
374
x: T,
@@ -346,11 +387,13 @@ <h2 id='generic-structs' class='section-header'><a href='#generic-structs'>Gener
346
387
}
347
388
}</ pre >
348
389
349
- < p > So far you’ve seen generics that take absolutely any type. These are useful in
350
- many cases: you’ve already seen < code > Option<T></ code > , and later you’ll meet universal
351
- container types like < a href ="../std/vec/struct.Vec.html "> < code > Vec<T></ code > </ a > . On the other hand, often you want to
352
- trade that flexibility for increased expressive power. Read about < a href ="traits.html "> trait
353
- bounds</ a > to see why and how.</ p >
390
+ <!-- So far you’ve seen generics that take absolutely any type. These are useful in
391
+ many cases: you’ve already seen `Option<T>`, and later you’ll meet universal
392
+ container types like [`Vec<T>`][Vec]. On the other hand, often you want to
393
+ trade that flexibility for increased expressive power. Read about [trait
394
+ bounds][traits] to see why and how. -->
395
+
396
+ < p > ここまででありとあらゆる型をとることのできるジェネリクスについて見てきました。多くの場合これらは有用です。 < code > Option<T></ code > は既に見た通りですし、のちに < code > Vec<T></ code > のような普遍的なコンテナ型を知ることになるでしょう。一方で、その柔軟性と引き換えに表現力を増加させたくなることもあります。それは何故か、そしてその方法を知るためには < a href ="traits.html "> トレイト境界</ a > を読んで下さい。</ p >
354
397
355
398
< script type ="text/javascript ">
356
399
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments