Skip to content

Commit 14e6348

Browse files
committed
Merge pull request #72 from pocket7878/associated-types
4.30. Associated Types
2 parents ea97a56 + 6de9397 commit 14e6348

File tree

2 files changed

+87
-47
lines changed

2 files changed

+87
-47
lines changed

1.6/ja/book/associated-types.md

Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
% Associated Types
2-
3-
Associated types are a powerful part of Rust’s type system. They’re related to
4-
the idea of a ‘type family’, in other words, grouping multiple types together. That
5-
description is a bit abstract, so let’s dive right into an example. If you want
6-
to write a `Graph` trait, you have two types to be generic over: the node type
7-
and the edge type. So you might write a trait, `Graph<N, E>`, that looks like
8-
this:
1+
% 関連型
2+
<!-- % Associated Types -->
3+
4+
<!-- Associated types are a powerful part of Rust’s type system. They’re related to -->
5+
<!-- the idea of a ‘type family’, in other words, grouping multiple types together. That -->
6+
<!-- description is a bit abstract, so let’s dive right into an example. If you want -->
7+
<!-- to write a `Graph` trait, you have two types to be generic over: the node type -->
8+
<!-- and the edge type. So you might write a trait, `Graph<N, E>`, that looks like -->
9+
<!-- this: -->
10+
関連型は、Rust型システムの強力な部分です。関連型は、「型族」という概念と関連が有り、
11+
言い換えると、複数の型をグループ化するものです。
12+
この説明はすこし抽象的なので、実際の例を見ていきましょう。
13+
例えば、 `Graph` トレイトを定義したいとしましょう、このときジェネリックになる2つの型: 頂点の型、辺の型 が存在します。
14+
そのため、以下のように `Graph<N, E>` と書きたくなるでしょう:
915

1016
```rust
1117
trait Graph<N, E> {
@@ -15,19 +21,24 @@ trait Graph<N, E> {
1521
}
1622
```
1723

18-
While this sort of works, it ends up being awkward. For example, any function
19-
that wants to take a `Graph` as a parameter now _also_ needs to be generic over
20-
the `N`ode and `E`dge types too:
24+
<!-- While this sort of works, it ends up being awkward. For example, any function -->
25+
<!-- that wants to take a `Graph` as a parameter now _also_ needs to be generic over -->
26+
<!-- the `N`ode and `E`dge types too: -->
27+
たしかに上のようなコードは動作しますが、この `Graph` の定義は少し扱いづらいです。
28+
たとえば、任意の `Graph` を引数に取る関数は、 _同時に_ 頂点 `N` と辺 `E` についてもジェネリックとなることになります:
2129

2230
```rust,ignore
2331
fn distance<N, E, G: Graph<N, E>>(graph: &G, start: &N, end: &N) -> u32 { ... }
2432
```
2533

26-
Our distance calculation works regardless of our `Edge` type, so the `E` stuff in
27-
this signature is just a distraction.
34+
<!-- Our distance calculation works regardless of our `Edge` type, so the `E` stuff in -->
35+
<!-- this signature is just a distraction. -->
36+
この距離を計算する関数distanceは、辺の型に関わらず動作します、そのためシグネチャに含まれる `E` に関連する部分は邪魔でしかありません。
2837

29-
What we really want to say is that a certain `E`dge and `N`ode type come together
30-
to form each kind of `Graph`. We can do that with associated types:
38+
<!-- What we really want to say is that a certain `E`dge and `N`ode type come together -->
39+
<!-- to form each kind of `Graph`. We can do that with associated types: -->
40+
本当に表現したいことは、それぞれのグラフ( `Graph` )は辺( `E` )や頂点( `N` )で構成されているということです。
41+
それは、以下のように関連型を用いて表現することができます:
3142

3243
```rust
3344
trait Graph {
@@ -40,19 +51,25 @@ trait Graph {
4051
}
4152
```
4253

43-
Now, our clients can be abstract over a given `Graph`:
54+
<!-- Now, our clients can be abstract over a given `Graph`: -->
55+
このようにすると、`Graph` を使った関数は以下のように書くことができます:
4456

4557
```rust,ignore
4658
fn distance<G: Graph>(graph: &G, start: &G::N, end: &G::N) -> u32 { ... }
4759
```
4860

49-
No need to deal with the `E`dge type here!
61+
<!-- No need to deal with the `E`dge type here! -->
62+
もう `E` について扱う必要はありません!
63+
5064

51-
Let’s go over all this in more detail.
65+
<!-- Let’s go over all this in more detail. -->
66+
もっと詳しく見ていきましょう。
5267

53-
## Defining associated types
68+
<!-- ## Defining associated types -->
69+
## 関連型を定義する
5470

55-
Let’s build that `Graph` trait. Here’s the definition:
71+
<!-- Let’s build that `Graph` trait. Here’s the definition: -->
72+
早速、`Graph` トレイトを定義しましょう。以下がその定義です:
5673

5774
```rust
5875
trait Graph {
@@ -64,12 +81,15 @@ trait Graph {
6481
}
6582
```
6683

67-
Simple enough. Associated types use the `type` keyword, and go inside the body
68-
of the trait, with the functions.
84+
<!-- Simple enough. Associated types use the `type` keyword, and go inside the body -->
85+
<!-- of the trait, with the functions. -->
86+
非常にシンプルですね。関連型には `type` キーワードを使い、そしてトレイトの本体や関数で利用します。
6987

70-
These `type` declarations can have all the same thing as functions do. For example,
71-
if we wanted our `N` type to implement `Display`, so we can print the nodes out,
72-
we could do this:
88+
<!-- These `type` declarations can have all the same thing as functions do. For example, -->
89+
<!-- if we wanted our `N` type to implement `Display`, so we can print the nodes out, -->
90+
<!-- we could do this: -->
91+
これらの `type` 宣言は、関数で利用できるものと同じものが全て利用できます。
92+
たとえば、 `N` 型が `Display` を実装していて欲しい時、つまり私達が頂点を出力したい時、以下のようにして指定することができます:
7393

7494
```rust
7595
use std::fmt;
@@ -83,10 +103,13 @@ trait Graph {
83103
}
84104
```
85105

86-
## Implementing associated types
106+
<!-- ## Implementing associated types -->
107+
## 関連型を実装する
87108

88-
Just like any trait, traits that use associated types use the `impl` keyword to
89-
provide implementations. Here’s a simple implementation of Graph:
109+
<!-- Just like any trait, traits that use associated types use the `impl` keyword to -->
110+
<!-- provide implementations. Here’s a simple implementation of Graph: -->
111+
通常のトレイトと同様に、関連型を使っているトレイトは実装するために `impl` を利用します。
112+
以下は、シンプルなGraphの実装例です:
90113

91114
```rust
92115
# trait Graph {
@@ -115,23 +138,34 @@ impl Graph for MyGraph {
115138
}
116139
```
117140

118-
This silly implementation always returns `true` and an empty `Vec<Edge>`, but it
119-
gives you an idea of how to implement this kind of thing. We first need three
120-
`struct`s, one for the graph, one for the node, and one for the edge. If it made
121-
more sense to use a different type, that would work as well, we’re just going to
122-
use `struct`s for all three here.
141+
<!-- This silly implementation always returns `true` and an empty `Vec<Edge>`, but it -->
142+
<!-- gives you an idea of how to implement this kind of thing. We first need three -->
143+
<!-- `struct`s, one for the graph, one for the node, and one for the edge. If it made -->
144+
<!-- more sense to use a different type, that would work as well, we’re just going to -->
145+
<!-- use `struct`s for all three here. -->
146+
この奇妙な実装は、つねに `true` と空の `Vec<Edge>` を返しますますが、どのように定義したら良いかのアイデアをくれます。
147+
まず、はじめに3つの `struct` が必要です、ひとつはグラフのため、そしてひとつは頂点のため、そしてもうひとつは辺のため。
148+
もし異なる型を利用することが適切ならば、そのようにすると良いでしょう、今回はこの3つの `struct` を用います。
149+
123150

124-
Next is the `impl` line, which is just like implementing any other trait.
151+
<!-- Next is the `impl` line, which is just like implementing any other trait. -->
152+
次は `impl` の行です、これは他のトレイトを実装するときと同様です。
125153

126-
From here, we use `=` to define our associated types. The name the trait uses
127-
goes on the left of the `=`, and the concrete type we’re `impl`ementing this
128-
for goes on the right. Finally, we use the concrete types in our function
129-
declarations.
154+
<!-- From here, we use `=` to define our associated types. The name the trait uses -->
155+
<!-- goes on the left of the `=`, and the concrete type we’re `impl`ementing this -->
156+
<!-- for goes on the right. Finally, we use the concrete types in our function -->
157+
<!-- declarations. -->
158+
そして、`=` を関連型を定義するために利用します。
159+
トレイトが利用する名前は `=` の左側にある名前で、実装に用いる具体的な型は右側にあるものになります。
160+
最後に、具体的な型を関数の宣言に利用します。
130161

131-
## Trait objects with associated types
162+
<!-- ## Trait objects with associated types -->
163+
## 関連型を伴うトレイト
132164

133-
There’s one more bit of syntax we should talk about: trait objects. If you
134-
try to create a trait object from an associated type, like this:
165+
<!-- There’s one more bit of syntax we should talk about: trait objects. If you -->
166+
<!-- try to create a trait object from an associated type, like this: -->
167+
すこし触れておきたい構文: トレイトオブジェクト が有ります。
168+
もし、トレイトオブジェクトを以下のように関連型から作成しようとした場合:
135169

136170
```rust,ignore
137171
# trait Graph {
@@ -157,7 +191,8 @@ let graph = MyGraph;
157191
let obj = Box::new(graph) as Box<Graph>;
158192
```
159193

160-
You’ll get two errors:
194+
<!-- You’ll get two errors: -->
195+
以下の様なエラーが発生します:
161196

162197
```text
163198
error: the value of the associated type `E` (from the trait `main::Graph`) must
@@ -170,8 +205,10 @@ let obj = Box::new(graph) as Box<Graph>;
170205
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171206
```
172207

173-
We can’t create a trait object like this, because we don’t know the associated
174-
types. Instead, we can write this:
208+
<!-- We can’t create a trait object like this, because we don’t know the associated -->
209+
<!-- types. Instead, we can write this: -->
210+
上のようにしてトレイトオブジェクトを作ることはできません、なぜなら関連型について知らないからです
211+
代わりに以下のように書くことができます:
175212

176213
```rust
177214
# trait Graph {
@@ -197,6 +234,8 @@ let graph = MyGraph;
197234
let obj = Box::new(graph) as Box<Graph<N=Node, E=Edge>>;
198235
```
199236

200-
The `N=Node` syntax allows us to provide a concrete type, `Node`, for the `N`
201-
type parameter. Same with `E=Edge`. If we didn’t provide this constraint, we
202-
couldn’t be sure which `impl` to match this trait object to.
237+
<!-- The `N=Node` syntax allows us to provide a concrete type, `Node`, for the `N` -->
238+
<!-- type parameter. Same with `E=Edge`. If we didn’t provide this constraint, we -->
239+
<!-- couldn’t be sure which `impl` to match this trait object to. -->
240+
`N=Node` 構文を用いて型パラメータ `N` にたいして具体的な型 `Node` を指定することができます、`E=Edge` についても同様です。
241+
もしこの制約を指定しなかった場合、このトレイトオブジェクトに対してどの `impl` がマッチするのか定まりません。

TranslationTable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
| tick | クオート
100100
| trait | トレイト
101101
| type inference | 型推論
102+
| type family | 型族
102103
| Universal Function Call Syntax | 共通の関数呼び出し構文
103104
| unsigned | 符号無し
104105
| unsized type | サイズ不定型

0 commit comments

Comments
 (0)