Skip to content

Commit 894077f

Browse files
committed
Update Generics to 1.9
- Update the contents. - Change the comment style to use the same style to other pages.
1 parent 66a52ad commit 894077f

File tree

2 files changed

+68
-68
lines changed

2 files changed

+68
-68
lines changed

1.9/ja/book/generics.md

Lines changed: 68 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
% ジェネリクス
22
<!-- % Generics -->
33

4-
<!-- Sometimes, when writing a function or data type, we may want it to work for
5-
multiple types of arguments. In Rust, we can do this with generics.
6-
Generics are called ‘parametric polymorphism’ in type theory,
7-
which means that they are types or functions that have multiple forms (‘poly’
8-
is multiple, ‘morph’ is form) over a given parameter (‘parametric’). -->
9-
時々、関数やデータ型を書いていると、引数が複数の型に対応したものが欲しくなることもあります。Rustでは、ジェネリクスを用いてこれを実現しています。ジェネリクスは型理論において「パラメトリック多相」(parametric polymorphism)と呼ばれ、与えられたパラメータにより(「parametric」)型もしくは関数が多数の様相(「poly」は多様、「morph」は様相を意味します)(訳注: ここで「様相」は型を指します)を持つことを意味しています。
10-
11-
<!-- Anyway, enough type theory, let’s check out some generic code. Rust’s
12-
standard library provides a type, `Option<T>`, that’s generic: -->
13-
さて、型理論はもう十分です。続いてジェネリックなコードを幾つか見ていきましょう。Rustが標準ライブラリで提供している型 `Option<T>` はジェネリックです。
4+
<!-- Sometimes, when writing a function or data type, we may want it to work for -->
5+
<!-- multiple types of arguments. In Rust, we can do this with generics. -->
6+
<!-- Generics are called ‘parametric polymorphism’ in type theory, -->
7+
<!-- which means that they are types or functions that have multiple forms (‘poly’ -->
8+
<!-- is multiple, ‘morph’ is form) over a given parameter (‘parametric’). -->
9+
時々、関数やデータ型を書いていると、引数が複数の型に対応したものが欲しくなることもあります。
10+
Rustでは、ジェネリクスを用いてこれを実現しています。
11+
ジェネリクスは型理論において「パラメトリック多相」(parametric polymorphism)と呼ばれ、与えられたパラメータにより(「parametric」)型もしくは関数が多数の様相(「poly」は多様、「morph」は様相を意味します)
12+
(訳注: ここで「様相」は型を指します)を持つことを意味しています。
13+
14+
<!-- Anyway, enough type theory, let’s check out some generic code. Rust’s -->
15+
<!-- standard library provides a type, `Option<T>`, that’s generic: -->
16+
さて、型理論はもう十分です。続いてジェネリックなコードを幾つか見ていきましょう。
17+
Rustが標準ライブラリで提供している型 `Option<T>` はジェネリックです。
1418

1519
```rust
1620
enum Option<T> {
@@ -19,32 +23,39 @@ enum Option<T> {
1923
}
2024
```
2125

22-
<!-- The `<T>` part, which you’ve seen a few times before, indicates that this is
23-
a generic data type. Inside the declaration of our `enum`, wherever we see a `T`,
24-
we substitute that type for the same type used in the generic. Here’s an
25-
example of using `Option<T>`, with some extra type annotations: -->
26-
`<T>` の部分は、前に少し見たことがあると思いますが、これがジェネリックなデータ型であることを示しています。 `enum` の宣言内であれば、どこでも `T` を使うことができ、宣言内に登場する同じ型をジェネリック内で `T` 型に置き換えています。型注釈を用いた`Option<T>`の使用例が以下になります。
26+
<!-- The `<T>` part, which you’ve seen a few times before, indicates that this is -->
27+
<!-- a generic data type. Inside the declaration of our `enum`, wherever we see a `T`, -->
28+
<!-- we substitute that type for the same type used in the generic. Here’s an -->
29+
<!-- example of using `Option<T>`, with some extra type annotations: -->
30+
`<T>` の部分は、前に少し見たことがあると思いますが、これがジェネリックなデータ型であることを示しています。
31+
`enum` の宣言内であれば、どこでも `T` を使うことができ、宣言内に登場する同じ型をジェネリック内で `T` 型に置き換えています。
32+
型注釈を用いた`Option<T>`の使用例が以下になります。
2733

2834
```rust
2935
let x: Option<i32> = Some(5);
3036
```
3137

32-
<!-- In the type declaration, we say `Option<i32>`. Note how similar this looks to
33-
`Option<T>`. So, in this particular `Option`, `T` has the value of `i32`. On
34-
the right-hand side of the binding, we make a `Some(T)`, where `T` is `5`.
35-
Since that’s an `i32`, the two sides match, and Rust is happy. If they didn’t
36-
match, we’d get an error: -->
37-
この型宣言では `Option<i32>` と書かれています。 `Option<T>` の違いに注目して下さい。そう、上記の `Option` では `T` の値は `i32` です。この束縛の右辺の `Some(T)` では、 `T``5` となります。それが `i32` なので、両辺の型が一致するため、Rustは満足します。型が不一致であれば、以下のようなエラーが発生します。
38+
<!-- In the type declaration, we say `Option<i32>`. Note how similar this looks to -->
39+
<!-- `Option<T>`. So, in this particular `Option`, `T` has the value of `i32`. On -->
40+
<!-- the right-hand side of the binding, we make a `Some(T)`, where `T` is `5`. -->
41+
<!-- Since that’s an `i32`, the two sides match, and Rust is happy. If they didn’t -->
42+
<!-- match, we’d get an error: -->
43+
この型宣言では `Option<i32>` と書かれています。
44+
`Option<T>` の違いに注目して下さい。そう、上記の `Option` では `T` の値は `i32` です。
45+
この束縛の右辺の `Some(T)` では、 `T``5` となります。
46+
それが `i32` なので、両辺の型が一致するため、Rustは満足します。
47+
型が不一致であれば、以下のようなエラーが発生します。
3848

3949
```rust,ignore
4050
let x: Option<f64> = Some(5);
4151
// error: mismatched types: expected `core::option::Option<f64>`,
4252
// found `core::option::Option<_>` (expected f64 but found integral variable)
4353
```
4454

45-
<!-- That doesn’t mean we can’t make `Option<T>`s that hold an `f64`! They just have
46-
to match up: -->
47-
これは `f64` を保持する `Option<T>` が作れないという意味ではありませんからね!リテラルと宣言の型をぴったり合わせなければなりません。
55+
<!-- That doesn’t mean we can’t make `Option<T>`s that hold an `f64`! They have -->
56+
<!-- to match up: -->
57+
これは `f64` を保持する `Option<T>` が作れないという意味ではありませんからね!
58+
リテラルと宣言の型をぴったり合わせなければなりません。
4859

4960
```rust
5061
let x: Option<i32> = Some(5);
@@ -55,7 +66,8 @@ let y: Option<f64> = Some(5.0f64);
5566
これだけで結構です。1つの定義で、多くの用途が得られます。
5667

5768
<!-- 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>`: -->
58-
ジェネリクスにおいてジェネリックな型は1つまで、といった制限はありません。Rustの標準ライブラリに入っている類似の型 `Result<T, E>` について考えてみます。
69+
ジェネリクスにおいてジェネリックな型は1つまで、といった制限はありません。
70+
Rustの標準ライブラリに入っている類似の型 `Result<T, E>` について考えてみます。
5971

6072
```rust
6173
enum Result<T, E> {
@@ -64,9 +76,11 @@ enum Result<T, E> {
6476
}
6577
```
6678

67-
<!-- This type is generic over _two_ types: `T` and `E`. By the way, the capital letters
68-
can be any letter you’d like. We could define `Result<T, E>` as: -->
69-
この型では `T``E`_2つ_ がジェネリックです。ちなみに、大文字の部分はあなたの好きな文字で構いません。もしあなたが望むなら `Result<T, E>` を、
79+
<!-- This type is generic over _two_ types: `T` and `E`. By the way, the capital letters -->
80+
<!-- can be any letter you’d like. We could define `Result<T, E>` as: -->
81+
この型では `T``E`_2つ_ がジェネリックです。
82+
ちなみに、大文字の部分はあなたの好きな文字で構いません。
83+
もしあなたが望むなら `Result<T, E>` を、
7084

7185
```rust
7286
enum Result<A, Z> {
@@ -75,12 +89,13 @@ enum Result<A, Z> {
7589
}
7690
```
7791

78-
<!-- if we wanted to. Convention says that the first generic parameter should be
79-
`T`, for ‘type’, and that we use `E` for ‘error’. Rust doesn’t care, however. -->
80-
のように定義できます。慣習としては、「Type」から第1ジェネリックパラメータは `T` であるべきですし、「Error」から `E` を用いるのですが、Rustは気にしません。
92+
<!-- if we wanted to. Convention says that the first generic parameter should be -->
93+
<!-- `T`, for ‘type’, and that we use `E` for ‘error’. Rust doesn’t care, however. -->
94+
のように定義できます。
95+
慣習としては、「Type」から第1ジェネリックパラメータは `T` であるべきですし、「Error」から `E` を用いるのですが、Rustは気にしません。
8196

82-
<!-- The `Result<T, E>` type is intended to be used to return the result of a
83-
computation, and to have the ability to return an error if it didn’t work out. -->
97+
<!-- The `Result<T, E>` type is intended to be used to return the result of a -->
98+
<!-- computation, and to have the ability to return an error if it didn’t work out. -->
8499
`Result<T, E>` 型は計算の結果を返すために使われることが想定されており、正常に動作しなかった場合にエラーの値を返す機能を持っています。
85100

86101
<!-- ## Generic functions -->
@@ -96,9 +111,10 @@ fn takes_anything<T>(x: T) {
96111
}
97112
```
98113

99-
<!-- The syntax has two parts: the `<T>` says “this function is generic over one
100-
type, `T`”, and the `x: T` says “x has the type `T`.” -->
101-
構文は2つのパーツから成ります。 `<T>` は「この関数は1つの型、 `T` に対してジェネリックである」ということであり、 `x: T` は「xは `T` 型である」という意味です。
114+
<!-- The syntax has two parts: the `<T>` says “this function is generic over one -->
115+
<!-- type, `T`”, and the `x: T` says “x has the type `T`.” -->
116+
構文は2つのパーツから成ります。
117+
`<T>` は「この関数は1つの型、 `T` に対してジェネリックである」ということであり、 `x: T` は「xは `T` 型である」という意味です。
102118

103119
<!-- Multiple arguments can have the same generic type: -->
104120
複数の引数が同じジェネリックな型を持つこともできます。
@@ -134,13 +150,13 @@ let int_origin = Point { x: 0, y: 0 };
134150
let float_origin = Point { x: 0.0, y: 0.0 };
135151
```
136152

137-
<!-- Similar to functions, the `<T>` is where we declare the generic parameters,
138-
and we then use `x: T` in the type declaration, too. -->
153+
<!-- Similar to functions, the `<T>` is where we declare the generic parameters, -->
154+
<!-- and we then use `x: T` in the type declaration, too. -->
139155
関数と同様に、 `<T>` がジェネリックパラメータを宣言する場所であり、型宣言において `x: T` を使うのも同じです。
140156

141-
<!-- When you want to add an implementation for the generic `struct`, you just
142-
declare the type parameter after the `impl`: -->
143-
ジェネリックな `struct` に実装を追加したい場合、 `impl` の後に型パラメータを宣言するだけです
157+
<!-- When you want to add an implementation for the generic `struct`, you -->
158+
<!-- declare the type parameter after the `impl`: -->
159+
ジェネリックな `struct` に実装を追加したい場合、 `impl` の後に型パラメータを宣言します
144160

145161
```rust
146162
# struct Point<T> {
@@ -155,12 +171,17 @@ impl<T> Point<T> {
155171
}
156172
```
157173

158-
<!-- So far you’ve seen generics that take absolutely any type. These are useful in
159-
many cases: you’ve already seen `Option<T>`, and later you’ll meet universal
160-
container types like [`Vec<T>`][Vec]. On the other hand, often you want to
161-
trade that flexibility for increased expressive power. Read about [trait
162-
bounds][traits] to see why and how. -->
163-
ここまででありとあらゆる型をとることのできるジェネリクスについて見てきました。多くの場合これらは有用です。 `Option<T>` は既に見た通りですし、のちに `Vec<T>` のような普遍的なコンテナ型を知ることになるでしょう。一方で、その柔軟性と引き換えに表現力を増加させたくなることもあります。それは何故か、そしてその方法を知るためには [トレイト境界][traits] を読んで下さい。
174+
<!-- So far you’ve seen generics that take absolutely any type. These are useful in -->
175+
<!-- many cases: you’ve already seen `Option<T>`, and later you’ll meet universal -->
176+
<!-- container types like [`Vec<T>`][Vec]. On the other hand, often you want to -->
177+
<!-- trade that flexibility for increased expressive power. Read about [trait -->
178+
<!-- bounds][traits] to see why and how. -->
179+
<!-- 訳注:and later you ... Vec(先にでてきてる)
180+
ここまででありとあらゆる型をとることのできるジェネリクスについて見てきました。
181+
多くの場合これらは有用です。
182+
`Option<T>` は既に見た通りですし、のちに `Vec<T>` のような普遍的なコンテナ型を知ることになるでしょう。
183+
一方で、その柔軟性と引き換えに表現力を増加させたくなることもあります。
184+
それは何故か、そしてその方法を知るためには [トレイト境界][traits] を読んで下さい。
164185
165186
[traits]: traits.html
166187
[Vec]: ../std/vec/struct.Vec.html

diff-1.6.0..1.9.0/src/doc/book/generics.md

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)