Skip to content

Commit 382ed89

Browse files
committed
generate
1 parent 73206a8 commit 382ed89

File tree

1 file changed

+92
-50
lines changed

1 file changed

+92
-50
lines changed

public/1.6/book/method-syntax.html

Lines changed: 92 additions & 50 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>Method Syntax</title>
7+
<title>メソッドシンタックス</title>
88

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

@@ -185,27 +185,39 @@
185185
<div id='page'>
186186

187187

188-
<h1 class="title">Method Syntax</h1>
189-
<p>Functions are great, but if you want to call a bunch of them on some data, it
190-
can be awkward. Consider this code:</p>
188+
<h1 class="title">メソッドシンタックス</h1>
189+
<!-- % Method Syntax -->
190+
191+
<!-- Functions are great, but if you want to call a bunch of them on some data, it
192+
can be awkward. Consider this code: -->
193+
194+
<p>関数は素晴らしいのですが、幾つかのデータに対し複数の関数をまとめて呼び出したい時、困ったことになります。以下のコードについて考えてみます。</p>
191195
<span class='rusttest'>fn main() {
192196
baz(bar(foo));
193197
}</span><pre class='rust rust-example-rendered'>
194198
<span class='ident'>baz</span>(<span class='ident'>bar</span>(<span class='ident'>foo</span>));</pre>
195199

196-
<p>We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the
200+
<!-- We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the
197201
order that the functions would get called in, that’s inside-out: ‘foo bar baz’.
198-
Wouldn’t it be nice if we could do this instead?</p>
202+
Wouldn’t it be nice if we could do this instead? -->
203+
204+
<p>私たちはこれを左から右へ、「baz bar foo」と読むことになりますが、関数が呼び出される順番は異なり、内側から外へ「foo bar baz」となります。もし代わりにこうできたらいいとは思いませんか?</p>
199205
<span class='rusttest'>fn main() {
200206
foo.bar().baz();
201207
}</span><pre class='rust rust-example-rendered'>
202208
<span class='ident'>foo</span>.<span class='ident'>bar</span>().<span class='ident'>baz</span>();</pre>
203209

204-
<p>Luckily, as you may have guessed with the leading question, you can! Rust provides
205-
the ability to use this ‘method call syntax’ via the <code>impl</code> keyword.</p>
210+
<!-- Luckily, as you may have guessed with the leading question, you can! Rust provides
211+
the ability to use this ‘method call syntax’ via the `impl` keyword. -->
212+
213+
<p>最初の質問でもう分かっているかもしれませんが、幸いにもこれは可能です!Rustは <code>impl</code> キーワードによってこの「メソッド呼び出し構文」の機能を提供しています。</p>
214+
215+
<!-- # Method calls -->
216+
217+
<h1 id='メソッド呼び出し' class='section-header'><a href='#メソッド呼び出し'>メソッド呼び出し</a></h1>
218+
<!-- Here’s how it works: -->
206219

207-
<h1 id='method-calls' class='section-header'><a href='#method-calls'>Method calls</a></h1>
208-
<p>Here’s how it works:</p>
220+
<p>どんな風に動作するかが以下になります。</p>
209221
<span class='rusttest'>struct Circle {
210222
x: f64,
211223
y: f64,
@@ -240,23 +252,31 @@ <h1 id='method-calls' class='section-header'><a href='#method-calls'>Method call
240252
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{}&quot;</span>, <span class='ident'>c</span>.<span class='ident'>area</span>());
241253
}</pre>
242254

243-
<p>This will print <code>12.566371</code>.</p>
255+
<!-- This will print `12.566371`. -->
244256

245-
<p>We’ve made a <code>struct</code> that represents a circle. We then write an <code>impl</code> block,
246-
and inside it, define a method, <code>area</code>.</p>
257+
<p>これは <code>12.566371</code> と出力します。</p>
247258

248-
<p>Methods take a special first parameter, of which there are three variants:
249-
<code>self</code>, <code>&amp;self</code>, and <code>&amp;mut self</code>. You can think of this first parameter as
250-
being the <code>foo</code> in <code>foo.bar()</code>. The three variants correspond to the three
251-
kinds of things <code>foo</code> could be: <code>self</code> if it’s just a value on the stack,
252-
<code>&amp;self</code> if it’s a reference, and <code>&amp;mut self</code> if it’s a mutable reference.
253-
Because we took the <code>&amp;self</code> parameter to <code>area</code>, we can use it just like any
254-
other parameter. Because we know it’s a <code>Circle</code>, we can access the <code>radius</code>
255-
just like we would with any other <code>struct</code>.</p>
259+
<!-- We’ve made a `struct` that represents a circle. We then write an `impl` block,
260+
and inside it, define a method, `area`. -->
256261

257-
<p>We should default to using <code>&amp;self</code>, as you should prefer borrowing over taking
262+
<p>私たちは円を表す <code>struct</code> を作りました。その際 <code>impl</code> ブロックを書き、その中に <code>area</code> というメソッドを定義しています。</p>
263+
264+
<!-- Methods take a special first parameter, of which there are three variants:
265+
`self`, `&self`, and `&mut self`. You can think of this first parameter as
266+
being the `foo` in `foo.bar()`. The three variants correspond to the three
267+
kinds of things `foo` could be: `self` if it’s just a value on the stack,
268+
`&self` if it’s a reference, and `&mut self` if it’s a mutable reference.
269+
Because we took the `&self` parameter to `area`, we can use it just like any
270+
other parameter. Because we know it’s a `Circle`, we can access the `radius`
271+
just like we would with any other `struct`. -->
272+
273+
<p>メソッドに渡す特別な第1引数として、 <code>self</code><code>&amp;self</code><code>&amp;mut self</code> という3つの変形があります。第一引数は <code>foo.bar()</code> に於ける <code>foo</code> だと考えて下さい。3つの変形は <code>foo</code> が成り得る3種類の状態に対応しており、それぞれ <code>self</code> がスタック上の値である場合、 <code>&amp;self</code> が参照である場合、 <code>&amp;mut self</code> がミュータブルな参照である場合となっています。 <code>area</code> では <code>&amp;self</code> を受け取っているため、他の引数と同じように扱えます。引数が <code>Circle</code> であるのは分かっていますから、他の <code>struct</code> でするように <code>radius</code> へアクセスできます。</p>
274+
275+
<!-- We should default to using `&self`, as you should prefer borrowing over taking
258276
ownership, as well as taking immutable references over mutable ones. Here’s an
259-
example of all three variants:</p>
277+
example of all three variants: -->
278+
279+
<p>所有権を渡すよりも借用を好んで使うべきなのは勿論のこと、ミュータブルな参照よりもイミュータブルな参照を渡すべきですから、 <code>&amp;self</code> を常用すべきです。以下が3種類全ての例です。</p>
260280
<span class='rusttest'>fn main() {
261281
struct Circle {
262282
x: f64,
@@ -298,8 +318,10 @@ <h1 id='method-calls' class='section-header'><a href='#method-calls'>Method call
298318
}
299319
}</pre>
300320

301-
<p>You can use as many <code>impl</code> blocks as you’d like. The previous example could
302-
have also been written like this:</p>
321+
<!--You can use as many `impl` blocks as you’d like. The previous example could
322+
have also been written like this: -->
323+
324+
<p>好きな数だけ <code>impl</code> ブロックを使用することができます。前述の例は以下のように書くこともできるでしょう。</p>
303325
<span class='rusttest'>fn main() {
304326
struct Circle {
305327
x: f64,
@@ -349,10 +371,14 @@ <h1 id='method-calls' class='section-header'><a href='#method-calls'>Method call
349371
}
350372
}</pre>
351373

352-
<h1 id='chaining-method-calls' class='section-header'><a href='#chaining-method-calls'>Chaining method calls</a></h1>
353-
<p>So, now we know how to call a method, such as <code>foo.bar()</code>. But what about our
354-
original example, <code>foo.bar().baz()</code>? This is called ‘method chaining’. Let’s
355-
look at an example:</p>
374+
<!-- # Chaining method calls -->
375+
376+
<h1 id='メソッドチェーン' class='section-header'><a href='#メソッドチェーン'>メソッドチェーン</a></h1>
377+
<!-- So, now we know how to call a method, such as `foo.bar()`. But what about our
378+
original example, `foo.bar().baz()`? This is called ‘method chaining’. Let’s
379+
look at an example: -->
380+
381+
<p>ここまでで、<code>foo.bar()</code> というようなメソッドの呼び出し方が分かりましたね。ですが元の例の <code>foo.bar().baz()</code> はどうなっているのでしょう?これは「メソッドチェーン」と呼ばれています。以下の例を見て下さい。</p>
356382
<span class='rusttest'>struct Circle {
357383
x: f64,
358384
y: f64,
@@ -401,7 +427,9 @@ <h1 id='chaining-method-calls' class='section-header'><a href='#chaining-method-
401427
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;{}&quot;</span>, <span class='ident'>d</span>);
402428
}</pre>
403429

404-
<p>Check the return type:</p>
430+
<!-- Check the return type: -->
431+
432+
<p>以下の返す型を確認して下さい。</p>
405433
<span class='rusttest'>fn main() {
406434
struct Circle;
407435
impl Circle {
@@ -410,12 +438,18 @@ <h1 id='chaining-method-calls' class='section-header'><a href='#chaining-method-
410438
}</span><pre class='rust rust-example-rendered'>
411439
<span class='kw'>fn</span> <span class='ident'>grow</span>(<span class='kw-2'>&amp;</span><span class='self'>self</span>, <span class='ident'>increment</span>: <span class='ident'>f64</span>) <span class='op'>-&gt;</span> <span class='ident'>Circle</span> {</pre>
412440

413-
<p>We just say we’re returning a <code>Circle</code>. With this method, we can grow a new
414-
<code>Circle</code> to any arbitrary size.</p>
441+
<!-- We just say we’re returning a `Circle`. With this method, we can grow a new
442+
`Circle` to any arbitrary size. -->
443+
444+
<p>単に <code>Circle</code> を返しているだけです。このメソッドにより、私たちは新しい <code>Circle</code> を任意の大きさに拡大することができます。</p>
445+
446+
<!-- # Associated functions -->
415447

416-
<h1 id='associated-functions' class='section-header'><a href='#associated-functions'>Associated functions</a></h1>
417-
<p>You can also define associated functions that do not take a <code>self</code> parameter.
418-
Here’s a pattern that’s very common in Rust code:</p>
448+
<h1 id='関連関数' class='section-header'><a href='#関連関数'>関連関数</a></h1>
449+
<!-- You can also define associated functions that do not take a `self` parameter.
450+
Here’s a pattern that’s very common in Rust code: -->
451+
452+
<p>あなたは <code>self</code> を引数に取らない関連関数を定義することもできます。以下のパターンはRustのコードにおいて非常にありふれた物です。</p>
419453
<span class='rusttest'>struct Circle {
420454
x: f64,
421455
y: f64,
@@ -456,17 +490,23 @@ <h1 id='associated-functions' class='section-header'><a href='#associated-functi
456490
<span class='kw'>let</span> <span class='ident'>c</span> <span class='op'>=</span> <span class='ident'>Circle</span>::<span class='ident'>new</span>(<span class='number'>0.0</span>, <span class='number'>0.0</span>, <span class='number'>2.0</span>);
457491
}</pre>
458492

459-
<p>This ‘associated function’ builds a new <code>Circle</code> for us. Note that associated
460-
functions are called with the <code>Struct::function()</code> syntax, rather than the
461-
<code>ref.method()</code> syntax. Some other languages call associated functions ‘static
462-
methods’.</p>
493+
<!-- This ‘associated function’ builds a new `Circle` for us. Note that associated
494+
functions are called with the `Struct::function()` syntax, rather than the
495+
`ref.method()` syntax. Some other languages call associated functions ‘static
496+
methods’. -->
497+
498+
<p>この「関連関数」(associated function)は新たに <code>Circle</code> を構築します。この関数は <code>ref.method()</code> ではなく、 <code>Struct::function()</code> という構文で呼び出されることに注意して下さい。幾つかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。</p>
499+
500+
<!-- # Builder Pattern -->
463501

464-
<h1 id='builder-pattern' class='section-header'><a href='#builder-pattern'>Builder Pattern</a></h1>
465-
<p>Let’s say that we want our users to be able to create <code>Circle</code>s, but we will
466-
allow them to only set the properties they care about. Otherwise, the <code>x</code>
467-
and <code>y</code> attributes will be <code>0.0</code>, and the <code>radius</code> will be <code>1.0</code>. Rust doesn’t
502+
<h1 id='builderパターン' class='section-header'><a href='#builderパターン'>Builderパターン</a></h1>
503+
<!-- Let’s say that we want our users to be able to create `Circle`s, but we will
504+
allow them to only set the properties they care about. Otherwise, the `x`
505+
and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesn’t
468506
have method overloading, named arguments, or variable arguments. We employ
469-
the builder pattern instead. It looks like this:</p>
507+
the builder pattern instead. It looks like this: -->
508+
509+
<p>ユーザが <code>Circle</code> を作成できるようにしつつも、書き換えたいプロパティだけを設定すれば良いようにしたいとしましょう。もし指定が無ければ <code>x</code><code>y</code><code>0.0</code><code>radius</code><code>1.0</code> であるものとします。Rustはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がない代わりにBuilderパターンを採用しており、それは以下のようになります。</p>
470510
<span class='rusttest'>struct Circle {
471511
x: f64,
472512
y: f64,
@@ -577,12 +617,14 @@ <h1 id='builder-pattern' class='section-header'><a href='#builder-pattern'>Build
577617
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>&quot;y: {}&quot;</span>, <span class='ident'>c</span>.<span class='ident'>y</span>);
578618
}</pre>
579619

580-
<p>What we’ve done here is make another <code>struct</code>, <code>CircleBuilder</code>. We’ve defined our
581-
builder methods on it. We’ve also defined our <code>area()</code> method on <code>Circle</code>. We
582-
also made one more method on <code>CircleBuilder</code>: <code>finalize()</code>. This method creates
583-
our final <code>Circle</code> from the builder. Now, we’ve used the type system to enforce
584-
our concerns: we can use the methods on <code>CircleBuilder</code> to constrain making
585-
<code>Circle</code>s in any way we choose.</p>
620+
<!-- What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our
621+
builder methods on it. We’ve also defined our `area()` method on `Circle`. We
622+
also made one more method on `CircleBuilder`: `finalize()`. This method creates
623+
our final `Circle` from the builder. Now, we’ve used the type system to enforce
624+
our concerns: we can use the methods on `CircleBuilder` to constrain making
625+
`Circle`s in any way we choose. -->
626+
627+
<p>ここではもう1つの <code>struct</code> である <code>CircleBuilder</code> を作成しています。その中にBuilderメソッドを定義しました。また <code>Circle</code><code>area()</code> メソッドを定義しました。 そして <code>CircleBuilder</code> にもう1つ <code>finalize()</code> というメソッドを作りました。このメソッドはBuilderから最終的な <code>Circle</code> を作成します。さて、先程の要求を実施するために型システムを使いました。 <code>CircleBuilder</code> のメソッドを好きなように組み合わせ、作る <code>Circle</code> への制約を与えることができます。</p>
586628

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

0 commit comments

Comments
 (0)