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 > Associated Types </ 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 "> Associated Types</ h1 >
189
- < p > Associated types are a powerful part of Rust’s type system. They’re related to
190
- the idea of a ‘type family’, in other words, grouping multiple types together. That
191
- description is a bit abstract, so let’s dive right into an example. If you want
192
- to write a < code > Graph</ code > trait, you have two types to be generic over: the node type
193
- and the edge type. So you might write a trait, < code > Graph<N, E></ code > , that looks like
194
- this:</ p >
188
+ < h1 class ="title "> 関連型</ h1 >
189
+ <!-- % Associated Types -->
190
+
191
+ <!-- Associated types are a powerful part of Rust’s type system. They’re related to -->
192
+
193
+ <!-- the idea of a ‘type family’, in other words, grouping multiple types together. That -->
194
+
195
+ <!-- description is a bit abstract, so let’s dive right into an example. If you want -->
196
+
197
+ <!-- to write a `Graph` trait, you have two types to be generic over: the node type -->
198
+
199
+ <!-- and the edge type. So you might write a trait, `Graph<N, E>`, that looks like -->
200
+
201
+ <!-- this: -->
202
+
203
+ < p > 関連型は、Rust型システムの強力な部分です。関連型は、「型族」という概念と関連が有り、
204
+ 言い換えると、複数の型をグループ化するものです。
205
+ この説明はすこし抽象的なので、実際の例を見ていきましょう。
206
+ 例えば、 < code > Graph</ code > トレイトを定義したいとしましょう、このときジェネリックになる2つの型: 頂点の型、辺の型 が存在します。
207
+ そのため、以下のように < code > Graph<N, E></ code > と書きたくなるでしょう:</ p >
195
208
< span class ='rusttest '> fn main() {
196
209
trait Graph<N, E> {
197
210
fn has_edge(&self, &N, &N) -> bool;
@@ -205,19 +218,31 @@ <h1 class="title">Associated Types</h1>
205
218
< span class ='comment '> // etc</ span >
206
219
}</ pre >
207
220
208
- < p > While this sort of works, it ends up being awkward. For example, any function
209
- that wants to take a < code > Graph</ code > as a parameter now < em > also</ em > needs to be generic over
210
- the < code > N</ code > ode and < code > E</ code > dge types too:</ p >
221
+ <!-- While this sort of works, it ends up being awkward. For example, any function -->
222
+
223
+ <!-- that wants to take a `Graph` as a parameter now _also_ needs to be generic over -->
224
+
225
+ <!-- the `N`ode and `E`dge types too: -->
226
+
227
+ < p > たしかに上のようなコードは動作しますが、この < code > Graph</ code > の定義は少し扱いづらいです。
228
+ たとえば、任意の < code > Graph</ code > を引数に取る関数は、 < em > 同時に</ em > 頂点 < code > N</ code > と辺 < code > E</ code > についてもジェネリックとなることになります:</ p >
211
229
< span class ='rusttest '> fn main() {
212
230
fn distance<N, E, G: Graph<N, E>>(graph: &G, start: &N, end: &N) -> u32 { ... }
213
231
}</ span > < pre class ='rust rust-example-rendered '>
214
232
< span class ='kw '> fn</ span > < span class ='ident '> distance</ span > < span class ='op '> <</ span > < span class ='ident '> N</ span > , < span class ='ident '> E</ span > , < span class ='ident '> G</ span > : < span class ='ident '> Graph</ span > < span class ='op '> <</ span > < span class ='ident '> N</ span > , < span class ='ident '> E</ span > < span class ='op '> >></ span > (< span class ='ident '> graph</ span > : < span class ='kw-2 '> &</ span > < span class ='ident '> G</ span > , < span class ='ident '> start</ span > : < span class ='kw-2 '> &</ span > < span class ='ident '> N</ span > , < span class ='ident '> end</ span > : < span class ='kw-2 '> &</ span > < span class ='ident '> N</ span > ) < span class ='op '> -></ span > < span class ='ident '> u32</ span > { ... }</ pre >
215
233
216
- < p > Our distance calculation works regardless of our < code > Edge</ code > type, so the < code > E</ code > stuff in
217
- this signature is just a distraction.</ p >
234
+ <!-- Our distance calculation works regardless of our `Edge` type, so the `E` stuff in -->
235
+
236
+ <!-- this signature is just a distraction. -->
237
+
238
+ < p > この距離を計算する関数distanceは、辺の型に関わらず動作します、そのためシグネチャに含まれる < code > E</ code > に関連する部分は邪魔でしかありません。</ p >
218
239
219
- < p > What we really want to say is that a certain < code > E</ code > dge and < code > N</ code > ode type come together
220
- to form each kind of < code > Graph</ code > . We can do that with associated types:</ p >
240
+ <!-- What we really want to say is that a certain `E`dge and `N`ode type come together -->
241
+
242
+ <!-- to form each kind of `Graph`. We can do that with associated types: -->
243
+
244
+ < p > 本当に表現したいことは、それぞれのグラフ( < code > Graph</ code > )は辺( < code > E</ code > )や頂点( < code > N</ code > )で構成されているということです。
245
+ それは、以下のように関連型を用いて表現することができます:</ p >
221
246
< span class ='rusttest '> fn main() {
222
247
trait Graph {
223
248
type N;
@@ -237,18 +262,28 @@ <h1 class="title">Associated Types</h1>
237
262
< span class ='comment '> // etc</ span >
238
263
}</ pre >
239
264
240
- < p > Now, our clients can be abstract over a given < code > Graph</ code > :</ p >
265
+ <!-- Now, our clients can be abstract over a given `Graph`: -->
266
+
267
+ < p > このようにすると、< code > Graph</ code > を使った関数は以下のように書くことができます:</ p >
241
268
< span class ='rusttest '> fn main() {
242
269
fn distance<G: Graph>(graph: &G, start: &G::N, end: &G::N) -> u32 { ... }
243
270
}</ span > < pre class ='rust rust-example-rendered '>
244
271
< span class ='kw '> fn</ span > < span class ='ident '> distance</ span > < span class ='op '> <</ span > < span class ='ident '> G</ span > : < span class ='ident '> Graph</ span > < span class ='op '> ></ span > (< span class ='ident '> graph</ span > : < span class ='kw-2 '> &</ span > < span class ='ident '> G</ span > , < span class ='ident '> start</ span > : < span class ='kw-2 '> &</ span > < span class ='ident '> G</ span > ::< span class ='ident '> N</ span > , < span class ='ident '> end</ span > : < span class ='kw-2 '> &</ span > < span class ='ident '> G</ span > ::< span class ='ident '> N</ span > ) < span class ='op '> -></ span > < span class ='ident '> u32</ span > { ... }</ pre >
245
272
246
- < p > No need to deal with the < code > E</ code > dge type here!</ p >
273
+ <!-- No need to deal with the `E`dge type here! -->
274
+
275
+ < p > もう < code > E</ code > について扱う必要はありません!</ p >
276
+
277
+ <!-- Let’s go over all this in more detail. -->
278
+
279
+ < p > もっと詳しく見ていきましょう。</ p >
247
280
248
- < p > Let’s go over all this in more detail. </ p >
281
+ <!-- ## Defining associated types -- >
249
282
250
- < h2 id ='defining-associated-types ' class ='section-header '> < a href ='#defining-associated-types '> Defining associated types</ a > </ h2 >
251
- < p > Let’s build that < code > Graph</ code > trait. Here’s the definition:</ p >
283
+ < h2 id ='関連型を定義する ' class ='section-header '> < a href ='#関連型を定義する '> 関連型を定義する</ a > </ h2 >
284
+ <!-- Let’s build that `Graph` trait. Here’s the definition: -->
285
+
286
+ < p > 早速、< code > Graph</ code > トレイトを定義しましょう。以下がその定義です:</ p >
252
287
< span class ='rusttest '> fn main() {
253
288
trait Graph {
254
289
type N;
@@ -266,12 +301,20 @@ <h2 id='defining-associated-types' class='section-header'><a href='#defining-ass
266
301
< span class ='kw '> fn</ span > < span class ='ident '> edges</ span > (< span class ='kw-2 '> &</ span > < span class ='self '> self</ span > , < span class ='kw-2 '> &</ span > < span class ='ident '> Self</ span > ::< span class ='ident '> N</ span > ) < span class ='op '> -></ span > < span class ='ident '> Vec</ span > < span class ='op '> <</ span > < span class ='ident '> Self</ span > ::< span class ='ident '> E</ span > < span class ='op '> ></ span > ;
267
302
}</ pre >
268
303
269
- < p > Simple enough. Associated types use the < code > type</ code > keyword, and go inside the body
270
- of the trait, with the functions.</ p >
304
+ <!-- Simple enough. Associated types use the `type` keyword, and go inside the body -->
305
+
306
+ <!-- of the trait, with the functions. -->
307
+
308
+ < p > 非常にシンプルですね。関連型には < code > type</ code > キーワードを使い、そしてトレイトの本体や関数で利用します。</ p >
309
+
310
+ <!-- These `type` declarations can have all the same thing as functions do. For example, -->
311
+
312
+ <!-- if we wanted our `N` type to implement `Display`, so we can print the nodes out, -->
271
313
272
- < p > These < code > type</ code > declarations can have all the same thing as functions do. For example,
273
- if we wanted our < code > N</ code > type to implement < code > Display</ code > , so we can print the nodes out,
274
- we could do this:</ p >
314
+ <!-- we could do this: -->
315
+
316
+ < p > これらの < code > type</ code > 宣言は、関数で利用できるものと同じものが全て利用できます。
317
+ たとえば、 < code > N</ code > 型が < code > Display</ code > を実装していて欲しい時、つまり私達が頂点を出力したい時、以下のようにして指定することができます:</ p >
275
318
< span class ='rusttest '> fn main() {
276
319
use std::fmt;
277
320
@@ -293,9 +336,15 @@ <h2 id='defining-associated-types' class='section-header'><a href='#defining-ass
293
336
< span class ='kw '> fn</ span > < span class ='ident '> edges</ span > (< span class ='kw-2 '> &</ span > < span class ='self '> self</ span > , < span class ='kw-2 '> &</ span > < span class ='ident '> Self</ span > ::< span class ='ident '> N</ span > ) < span class ='op '> -></ span > < span class ='ident '> Vec</ span > < span class ='op '> <</ span > < span class ='ident '> Self</ span > ::< span class ='ident '> E</ span > < span class ='op '> ></ span > ;
294
337
}</ pre >
295
338
296
- < h2 id ='implementing-associated-types ' class ='section-header '> < a href ='#implementing-associated-types '> Implementing associated types</ a > </ h2 >
297
- < p > Just like any trait, traits that use associated types use the < code > impl</ code > keyword to
298
- provide implementations. Here’s a simple implementation of Graph:</ p >
339
+ <!-- ## Implementing associated types -->
340
+
341
+ < h2 id ='関連型を実装する ' class ='section-header '> < a href ='#関連型を実装する '> 関連型を実装する</ a > </ h2 >
342
+ <!-- Just like any trait, traits that use associated types use the `impl` keyword to -->
343
+
344
+ <!-- provide implementations. Here’s a simple implementation of Graph: -->
345
+
346
+ < p > 通常のトレイトと同様に、関連型を使っているトレイトは実装するために < code > impl</ code > を利用します。
347
+ 以下は、シンプルなGraphの実装例です:</ p >
299
348
< span class ='rusttest '> fn main() {
300
349
trait Graph {
301
350
type N;
@@ -341,22 +390,45 @@ <h2 id='implementing-associated-types' class='section-header'><a href='#implemen
341
390
}
342
391
}</ pre >
343
392
344
- < p > This silly implementation always returns < code > true</ code > and an empty < code > Vec<Edge></ code > , but it
345
- gives you an idea of how to implement this kind of thing. We first need three
346
- < code > struct</ code > s, one for the graph, one for the node, and one for the edge. If it made
347
- more sense to use a different type, that would work as well, we’re just going to
348
- use < code > struct</ code > s for all three here.</ p >
393
+ <!-- This silly implementation always returns `true` and an empty `Vec<Edge>`, but it -->
394
+
395
+ <!-- gives you an idea of how to implement this kind of thing. We first need three -->
396
+
397
+ <!-- `struct`s, one for the graph, one for the node, and one for the edge. If it made -->
398
+
399
+ <!-- more sense to use a different type, that would work as well, we’re just going to -->
400
+
401
+ <!-- use `struct`s for all three here. -->
402
+
403
+ < p > この奇妙な実装は、つねに < code > true</ code > と空の < code > Vec<Edge></ code > を返しますますが、どのように定義したら良いかのアイデアをくれます。
404
+ まず、はじめに3つの < code > struct</ code > が必要です、ひとつはグラフのため、そしてひとつは頂点のため、そしてもうひとつは辺のため。
405
+ もし異なる型を利用することが適切ならば、そのようにすると良いでしょう、今回はこの3つの < code > struct</ code > を用います。</ p >
406
+
407
+ <!-- Next is the `impl` line, which is just like implementing any other trait. -->
408
+
409
+ < p > 次は < code > impl</ code > の行です、これは他のトレイトを実装するときと同様です。</ p >
410
+
411
+ <!-- From here, we use `=` to define our associated types. The name the trait uses -->
349
412
350
- < p > Next is the < code > impl </ code > line, which is just like implementing any other trait. </ p >
413
+ <!-- goes on the left of the `=`, and the concrete type we’re `impl`ementing this -- >
351
414
352
- < p > From here, we use < code > =</ code > to define our associated types. The name the trait uses
353
- goes on the left of the < code > =</ code > , and the concrete type we’re < code > impl</ code > ementing this
354
- for goes on the right. Finally, we use the concrete types in our function
355
- declarations.</ p >
415
+ <!-- for goes on the right. Finally, we use the concrete types in our function -->
356
416
357
- < h2 id ='trait-objects-with-associated-types ' class ='section-header '> < a href ='#trait-objects-with-associated-types '> Trait objects with associated types</ a > </ h2 >
358
- < p > There’s one more bit of syntax we should talk about: trait objects. If you
359
- try to create a trait object from an associated type, like this:</ p >
417
+ <!-- declarations. -->
418
+
419
+ < p > そして、< code > =</ code > を関連型を定義するために利用します。
420
+ トレイトが利用する名前は < code > =</ code > の左側にある名前で、実装に用いる具体的な型は右側にあるものになります。
421
+ 最後に、具体的な型を関数の宣言に利用します。</ p >
422
+
423
+ <!-- ## Trait objects with associated types -->
424
+
425
+ < h2 id ='関連型を伴うトレイト ' class ='section-header '> < a href ='#関連型を伴うトレイト '> 関連型を伴うトレイト</ a > </ h2 >
426
+ <!-- There’s one more bit of syntax we should talk about: trait objects. If you -->
427
+
428
+ <!-- try to create a trait object from an associated type, like this: -->
429
+
430
+ < p > すこし触れておきたい構文: トレイトオブジェクト が有ります。
431
+ もし、トレイトオブジェクトを以下のように関連型から作成しようとした場合:</ p >
360
432
< span class ='rusttest '> fn main() {
361
433
trait Graph {
362
434
type N;
@@ -383,7 +455,9 @@ <h2 id='trait-objects-with-associated-types' class='section-header'><a href='#tr
383
455
< span class ='kw '> let</ span > < span class ='ident '> graph</ span > < span class ='op '> =</ span > < span class ='ident '> MyGraph</ span > ;
384
456
< span class ='kw '> let</ span > < span class ='ident '> obj</ span > < span class ='op '> =</ span > < span class ='ident '> Box</ span > ::< span class ='ident '> new</ span > (< span class ='ident '> graph</ span > ) < span class ='kw '> as</ span > < span class ='ident '> Box</ span > < span class ='op '> <</ span > < span class ='ident '> Graph</ span > < span class ='op '> ></ span > ;</ pre >
385
457
386
- < p > You’ll get two errors:</ p >
458
+ <!-- You’ll get two errors: -->
459
+
460
+ < p > 以下の様なエラーが発生します:</ p >
387
461
388
462
< pre > < code class ="language-text "> error: the value of the associated type `E` (from the trait `main::Graph`) must
389
463
be specified [E0191]
@@ -395,8 +469,12 @@ <h2 id='trait-objects-with-associated-types' class='section-header'><a href='#tr
395
469
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
396
470
</ code > </ pre >
397
471
398
- < p > We can’t create a trait object like this, because we don’t know the associated
399
- types. Instead, we can write this:</ p >
472
+ <!-- We can’t create a trait object like this, because we don’t know the associated -->
473
+
474
+ <!-- types. Instead, we can write this: -->
475
+
476
+ < p > 上のようにしてトレイトオブジェクトを作ることはできません、なぜなら関連型について知らないからです
477
+ 代わりに以下のように書くことができます:</ p >
400
478
< span class ='rusttest '> fn main() {
401
479
trait Graph {
402
480
type N;
@@ -423,9 +501,14 @@ <h2 id='trait-objects-with-associated-types' class='section-header'><a href='#tr
423
501
< span class ='kw '> let</ span > < span class ='ident '> graph</ span > < span class ='op '> =</ span > < span class ='ident '> MyGraph</ span > ;
424
502
< span class ='kw '> let</ span > < span class ='ident '> obj</ span > < span class ='op '> =</ span > < span class ='ident '> Box</ span > ::< span class ='ident '> new</ span > (< span class ='ident '> graph</ span > ) < span class ='kw '> as</ span > < span class ='ident '> Box</ span > < span class ='op '> <</ span > < span class ='ident '> Graph</ span > < span class ='op '> <</ span > < span class ='ident '> N</ span > < span class ='op '> =</ span > < span class ='ident '> Node</ span > , < span class ='ident '> E</ span > < span class ='op '> =</ span > < span class ='ident '> Edge</ span > < span class ='op '> >></ span > ;</ pre >
425
503
426
- < p > The < code > N=Node</ code > syntax allows us to provide a concrete type, < code > Node</ code > , for the < code > N</ code >
427
- type parameter. Same with < code > E=Edge</ code > . If we didn’t provide this constraint, we
428
- couldn’t be sure which < code > impl</ code > to match this trait object to.</ p >
504
+ <!-- The `N=Node` syntax allows us to provide a concrete type, `Node`, for the `N` -->
505
+
506
+ <!-- type parameter. Same with `E=Edge`. If we didn’t provide this constraint, we -->
507
+
508
+ <!-- couldn’t be sure which `impl` to match this trait object to. -->
509
+
510
+ < p > < code > N=Node</ code > 構文を用いて型パラメータ < code > N</ code > にたいして具体的な型 < code > Node</ code > を指定することができます、< code > E=Edge</ code > についても同様です。
511
+ もしこの制約を指定しなかった場合、このトレイトオブジェクトに対してどの < code > impl</ code > がマッチするのか定まりません。</ p >
429
512
430
513
< script type ="text/javascript ">
431
514
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments