Skip to content

Commit 0e89538

Browse files
committed
generate
1 parent f930107 commit 0e89538

File tree

2 files changed

+92
-49
lines changed

2 files changed

+92
-49
lines changed

public/1.6/book/generics.html

Lines changed: 86 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<meta name="generator" content="rustdoc">
7-
<title>Generics</title>
7+
<title>ジェネリクス</title>
88

99
<link rel="stylesheet" type="text/css" href="rustbook.css">
1010

@@ -185,15 +185,21 @@
185185
<div id='page'>
186186

187187

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
190192
multiple types of arguments. In Rust, we can do this with generics.
191193
Generics are called ‘parametric polymorphism’ in type theory,
192194
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: -->
194201

195-
<p>Anyway, enough type theory, let’s check out some generic code. Rust’s
196-
standard library provides a type, <code>Option&lt;T&gt;</code>, that’s generic:</p>
202+
<p>さて、型理論はもう十分です。続いてジェネリックなコードを幾つか見ていきましょう。Rustが標準ライブラリで提供している型 <code>Option&lt;T&gt;</code> はジェネリックです。</p>
197203
<span class='rusttest'>fn main() {
198204
enum Option&lt;T&gt; {
199205
Some(T),
@@ -205,20 +211,24 @@ <h1 class="title">Generics</h1>
205211
<span class='prelude-val'>None</span>,
206212
}</pre>
207213

208-
<p>The <code>&lt;T&gt;</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`,
210216
we substitute that type for the same type used in the generic. Here’s an
211-
example of using <code>Option&lt;T&gt;</code>, with some extra type annotations:</p>
217+
example of using `Option<T>`, with some extra type annotations: -->
218+
219+
<p><code>&lt;T&gt;</code> の部分は、前に少し見たことがあると思いますが、これがジェネリックなデータ型であることを示しています。 <code>enum</code> の宣言内であれば、どこでも <code>T</code> を使うことができ、宣言内に登場する同じ型をジェネリック内で <code>T</code> 型に置き換えています。型注釈を用いた<code>Option&lt;T&gt;</code>の使用例が以下になります。</p>
212220
<span class='rusttest'>fn main() {
213221
let x: Option&lt;i32&gt; = Some(5);
214222
}</span><pre class='rust rust-example-rendered'>
215223
<span class='kw'>let</span> <span class='ident'>x</span>: <span class='prelude-ty'>Option</span><span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='prelude-val'>Some</span>(<span class='number'>5</span>);</pre>
216224

217-
<p>In the type declaration, we say <code>Option&lt;i32&gt;</code>. Note how similar this looks to
218-
<code>Option&lt;T&gt;</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&lt;i32&gt;</code> と書かれています。 <code>Option&lt;T&gt;</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>
222232
<span class='rusttest'>fn main() {
223233
let x: Option&lt;f64&gt; = Some(5);
224234
// error: mismatched types: expected `core::option::Option&lt;f64&gt;`,
@@ -228,18 +238,24 @@ <h1 class="title">Generics</h1>
228238
<span class='comment'>// error: mismatched types: expected `core::option::Option&lt;f64&gt;`,</span>
229239
<span class='comment'>// found `core::option::Option&lt;_&gt;` (expected f64 but found integral variable)</span></pre>
230240

231-
<p>That doesn’t mean we can’t make <code>Option&lt;T&gt;</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&lt;T&gt;</code> が作れないという意味ではありませんからね!リテラルと宣言の型をぴったり合わせなければなりません。</p>
233245
<span class='rusttest'>fn main() {
234246
let x: Option&lt;i32&gt; = Some(5);
235247
let y: Option&lt;f64&gt; = Some(5.0f64);
236248
}</span><pre class='rust rust-example-rendered'>
237249
<span class='kw'>let</span> <span class='ident'>x</span>: <span class='prelude-ty'>Option</span><span class='op'>&lt;</span><span class='ident'>i32</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='prelude-val'>Some</span>(<span class='number'>5</span>);
238250
<span class='kw'>let</span> <span class='ident'>y</span>: <span class='prelude-ty'>Option</span><span class='op'>&lt;</span><span class='ident'>f64</span><span class='op'>&gt;</span> <span class='op'>=</span> <span class='prelude-val'>Some</span>(<span class='number'>5.0f64</span>);</pre>
239251

240-
<p>This is just fine. One definition, multiple uses.</p>
252+
<!-- This is just fine. One definition, multiple uses. -->
241253

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&lt;T, E&gt;</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&lt;T, E&gt;</code> について考えてみます。</p>
243259
<span class='rusttest'>fn main() {
244260
enum Result&lt;T, E&gt; {
245261
Ok(T),
@@ -251,8 +267,10 @@ <h1 class="title">Generics</h1>
251267
<span class='prelude-val'>Err</span>(<span class='ident'>E</span>),
252268
}</pre>
253269

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&lt;T, E&gt;</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&lt;T, E&gt;</code> を、</p>
256274
<span class='rusttest'>fn main() {
257275
enum Result&lt;A, Z&gt; {
258276
Ok(A),
@@ -264,27 +282,40 @@ <h1 class="title">Generics</h1>
264282
<span class='prelude-val'>Err</span>(<span class='ident'>Z</span>),
265283
}</pre>
266284

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. -->
269292

270-
<p>The <code>Result&lt;T, E&gt;</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&lt;T, E&gt;</code> 型は計算の結果を返すために使われることが想定されており、正常に動作しなかった場合にエラーの値を返す機能を持っています。</p>
272294

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>
275301
<span class='rusttest'>fn main() {
276302
fn takes_anything&lt;T&gt;(x: T) {
277-
// do something with x
303+
// do something with x
304+
// xで何か行う
278305
}
279306
}</span><pre class='rust rust-example-rendered'>
280307
<span class='kw'>fn</span> <span class='ident'>takes_anything</span><span class='op'>&lt;</span><span class='ident'>T</span><span class='op'>&gt;</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>
282309
}</pre>
283310

284-
<p>The syntax has two parts: the <code>&lt;T&gt;</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>&lt;T&gt;</code> は「この関数は1つの型、 <code>T</code> に対してジェネリックである」ということであり、 <code>x: T</code> は「xは <code>T</code> 型である」という意味です。</p>
286315

287-
<p>Multiple arguments can have the same generic type:</p>
316+
<!-- Multiple arguments can have the same generic type: -->
317+
318+
<p>複数の引数が同じジェネリックな型を持つこともできます。</p>
288319
<span class='rusttest'>fn main() {
289320
fn takes_two_of_the_same_things&lt;T&gt;(x: T, y: T) {
290321
// ...
@@ -294,7 +325,9 @@ <h2 id='generic-functions' class='section-header'><a href='#generic-functions'>G
294325
<span class='comment'>// ...</span>
295326
}</pre>
296327

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>
298331
<span class='rusttest'>fn main() {
299332
fn takes_two_things&lt;T, U&gt;(x: T, y: U) {
300333
// ...
@@ -304,8 +337,12 @@ <h2 id='generic-functions' class='section-header'><a href='#generic-functions'>G
304337
<span class='comment'>// ...</span>
305338
}</pre>
306339

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>
309346
<span class='rusttest'>fn main() {
310347
struct Point&lt;T&gt; {
311348
x: T,
@@ -323,11 +360,15 @@ <h2 id='generic-structs' class='section-header'><a href='#generic-structs'>Gener
323360
<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> };
324361
<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>
325362

326-
<p>Similar to functions, the <code>&lt;T&gt;</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>&lt;T&gt;</code> がジェネリックパラメータを宣言する場所であり、型宣言において <code>x: T</code> を使うのも同じです。</p>
328367

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>
331372
<span class='rusttest'>fn main() {
332373
struct Point&lt;T&gt; {
333374
x: T,
@@ -346,11 +387,13 @@ <h2 id='generic-structs' class='section-header'><a href='#generic-structs'>Gener
346387
}
347388
}</pre>
348389

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&lt;T&gt;</code>, and later you’ll meet universal
351-
container types like <a href="../std/vec/struct.Vec.html"><code>Vec&lt;T&gt;</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&lt;T&gt;</code> は既に見た通りですし、のちに <code>Vec&lt;T&gt;</code> のような普遍的なコンテナ型を知ることになるでしょう。一方で、その柔軟性と引き換えに表現力を増加させたくなることもあります。それは何故か、そしてその方法を知るためには <a href="traits.html">トレイト境界</a> を読んで下さい。</p>
354397

355398
<script type="text/javascript">
356399
window.playgroundUrl = "https://play.rust-lang.org";

0 commit comments

Comments
 (0)