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 > Method Syntax </ 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 "> 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 >
191
195
< span class ='rusttest '> fn main() {
192
196
baz(bar(foo));
193
197
}</ span > < pre class ='rust rust-example-rendered '>
194
198
< span class ='ident '> baz</ span > (< span class ='ident '> bar</ span > (< span class ='ident '> foo</ span > ));</ pre >
195
199
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
197
201
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 >
199
205
< span class ='rusttest '> fn main() {
200
206
foo.bar().baz();
201
207
}</ span > < pre class ='rust rust-example-rendered '>
202
208
< span class ='ident '> foo</ span > .< span class ='ident '> bar</ span > ().< span class ='ident '> baz</ span > ();</ pre >
203
209
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: -->
206
219
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 >
209
221
< span class ='rusttest '> struct Circle {
210
222
x: f64,
211
223
y: f64,
@@ -240,23 +252,31 @@ <h1 id='method-calls' class='section-header'><a href='#method-calls'>Method call
240
252
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "{}"</ span > , < span class ='ident '> c</ span > .< span class ='ident '> area</ span > ());
241
253
}</ pre >
242
254
243
- < p > This will print < code > 12.566371</ code > . </ p >
255
+ <!-- This will print ` 12.566371`. -- >
244
256
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 >
247
258
248
- < p > Methods take a special first parameter, of which there are three variants:
249
- < code > self</ code > , < code > &self</ code > , and < code > &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 > &self</ code > if it’s a reference, and < code > &mut self</ code > if it’s a mutable reference.
253
- Because we took the < code > &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`. -->
256
261
257
- < p > We should default to using < code > &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 > &self</ code > 、 < code > &mut self</ code > という3つの変形があります。第一引数は < code > foo.bar()</ code > に於ける < code > foo</ code > だと考えて下さい。3つの変形は < code > foo</ code > が成り得る3種類の状態に対応しており、それぞれ < code > self</ code > がスタック上の値である場合、 < code > &self</ code > が参照である場合、 < code > &mut self</ code > がミュータブルな参照である場合となっています。 < code > area</ code > では < code > &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
258
276
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 > &self</ code > を常用すべきです。以下が3種類全ての例です。</ p >
260
280
< span class ='rusttest '> fn main() {
261
281
struct Circle {
262
282
x: f64,
@@ -298,8 +318,10 @@ <h1 id='method-calls' class='section-header'><a href='#method-calls'>Method call
298
318
}
299
319
}</ pre >
300
320
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 >
303
325
< span class ='rusttest '> fn main() {
304
326
struct Circle {
305
327
x: f64,
@@ -349,10 +371,14 @@ <h1 id='method-calls' class='section-header'><a href='#method-calls'>Method call
349
371
}
350
372
}</ pre >
351
373
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 >
356
382
< span class ='rusttest '> struct Circle {
357
383
x: f64,
358
384
y: f64,
@@ -401,7 +427,9 @@ <h1 id='chaining-method-calls' class='section-header'><a href='#chaining-method-
401
427
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "{}"</ span > , < span class ='ident '> d</ span > );
402
428
}</ pre >
403
429
404
- < p > Check the return type:</ p >
430
+ <!-- Check the return type: -->
431
+
432
+ < p > 以下の返す型を確認して下さい。</ p >
405
433
< span class ='rusttest '> fn main() {
406
434
struct Circle;
407
435
impl Circle {
@@ -410,12 +438,18 @@ <h1 id='chaining-method-calls' class='section-header'><a href='#chaining-method-
410
438
}</ span > < pre class ='rust rust-example-rendered '>
411
439
< span class ='kw '> fn</ span > < span class ='ident '> grow</ span > (< span class ='kw-2 '> &</ span > < span class ='self '> self</ span > , < span class ='ident '> increment</ span > : < span class ='ident '> f64</ span > ) < span class ='op '> -></ span > < span class ='ident '> Circle</ span > {</ pre >
412
440
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 -->
415
447
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 >
419
453
< span class ='rusttest '> struct Circle {
420
454
x: f64,
421
455
y: f64,
@@ -456,17 +490,23 @@ <h1 id='associated-functions' class='section-header'><a href='#associated-functi
456
490
< 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 > );
457
491
}</ pre >
458
492
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 -->
463
501
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
468
506
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 >
470
510
< span class ='rusttest '> struct Circle {
471
511
x: f64,
472
512
y: f64,
@@ -577,12 +617,14 @@ <h1 id='builder-pattern' class='section-header'><a href='#builder-pattern'>Build
577
617
< span class ='macro '> println</ span > < span class ='macro '> !</ span > (< span class ='string '> "y: {}"</ span > , < span class ='ident '> c</ span > .< span class ='ident '> y</ span > );
578
618
}</ pre >
579
619
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 >
586
628
587
629
< script type ="text/javascript ">
588
630
window . playgroundUrl = "https://play.rust-lang.org" ;
0 commit comments