1
- % Method Syntax
1
+ % メソッドシンタックス
2
+ <!-- % Method Syntax -->
2
3
3
- Functions are great, but if you want to call a bunch of them on some data, it
4
- can be awkward. Consider this code:
4
+ <!-- Functions are great, but if you want to call a bunch of them on some data, it
5
+ can be awkward. Consider this code: -->
6
+ 関数は素晴らしいのですが、幾つかのデータに対し複数の関数をまとめて呼び出したい時、困ったことになります。以下のコードについて考えてみます。
5
7
6
8
``` rust,ignore
7
9
baz(bar(foo));
8
10
```
9
11
10
- We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the
12
+ <!-- We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the
11
13
order that the functions would get called in, that’s inside-out: ‘foo bar baz’.
12
- Wouldn’t it be nice if we could do this instead?
14
+ Wouldn’t it be nice if we could do this instead? -->
15
+ 私たちはこれを左から右へ、「baz bar foo」と読むことになりますが、関数が呼び出される順番は異なり、内側から外へ「foo bar baz」となります。もし代わりにこうできたらいいとは思いませんか?
13
16
14
17
``` rust,ignore
15
18
foo.bar().baz();
16
19
```
17
20
18
- Luckily, as you may have guessed with the leading question, you can! Rust provides
19
- the ability to use this ‘method call syntax’ via the ` impl ` keyword.
21
+ <!-- Luckily, as you may have guessed with the leading question, you can! Rust provides
22
+ the ability to use this ‘method call syntax’ via the `impl` keyword. -->
23
+ 最初の質問でもう分かっているかもしれませんが、幸いにもこれは可能です!Rustは ` impl ` キーワードによってこの「メソッド呼び出し構文」の機能を提供しています。
20
24
21
- # Method calls
25
+ <!-- # Method calls -->
26
+ # メソッド呼び出し
22
27
23
- Here’s how it works:
28
+ <!-- Here’s how it works: -->
29
+ どんな風に動作するかが以下になります。
24
30
25
31
``` rust
26
32
struct Circle {
@@ -41,23 +47,27 @@ fn main() {
41
47
}
42
48
```
43
49
44
- This will print ` 12.566371 ` .
50
+ <!-- This will print `12.566371`. -->
51
+ これは ` 12.566371 ` と出力します。
45
52
46
- We’ve made a ` struct ` that represents a circle. We then write an ` impl ` block,
47
- and inside it, define a method, ` area ` .
53
+ <!-- We’ve made a `struct` that represents a circle. We then write an `impl` block,
54
+ and inside it, define a method, `area`. -->
55
+ 私たちは円を表す ` struct ` を作りました。その際 ` impl ` ブロックを書き、その中に ` area ` というメソッドを定義しています。
48
56
49
- Methods take a special first parameter, of which there are three variants:
57
+ <!-- Methods take a special first parameter, of which there are three variants:
50
58
`self`, `&self`, and `&mut self`. You can think of this first parameter as
51
59
being the `foo` in `foo.bar()`. The three variants correspond to the three
52
60
kinds of things `foo` could be: `self` if it’s just a value on the stack,
53
61
`&self` if it’s a reference, and `&mut self` if it’s a mutable reference.
54
62
Because we took the `&self` parameter to `area`, we can use it just like any
55
63
other parameter. Because we know it’s a `Circle`, we can access the `radius`
56
- just like we would with any other ` struct ` .
64
+ just like we would with any other `struct`. -->
65
+ メソッドに渡す特別な第1引数として、 ` self ` 、 ` &self ` 、 ` &mut self ` という3つの変形があります。第一引数は ` foo.bar() ` に於ける ` foo ` だと考えて下さい。3つの変形は ` foo ` が成り得る3種類の状態に対応しており、それぞれ ` self ` がスタック上の値である場合、 ` &self ` が参照である場合、 ` &mut self ` がミュータブルな参照である場合となっています。 ` area ` では ` &self ` を受け取っているため、他の引数と同じように扱えます。引数が ` Circle ` であるのは分かっていますから、他の ` struct ` でするように ` radius ` へアクセスできます。
57
66
58
- We should default to using ` &self ` , as you should prefer borrowing over taking
67
+ <!-- We should default to using `&self`, as you should prefer borrowing over taking
59
68
ownership, as well as taking immutable references over mutable ones. Here’s an
60
- example of all three variants:
69
+ example of all three variants: -->
70
+ 所有権を渡すよりも借用を好んで使うべきなのは勿論のこと、ミュータブルな参照よりもイミュータブルな参照を渡すべきですから、 ` &self ` を常用すべきです。以下が3種類全ての例です。
61
71
62
72
``` rust
63
73
struct Circle {
@@ -81,8 +91,9 @@ impl Circle {
81
91
}
82
92
```
83
93
84
- You can use as many ` impl ` blocks as you’d like. The previous example could
85
- have also been written like this:
94
+ <!-- You can use as many `impl` blocks as you’d like. The previous example could
95
+ have also been written like this: -->
96
+ 好きな数だけ ` impl ` ブロックを使用することができます。前述の例は以下のように書くこともできるでしょう。
86
97
87
98
``` rust
88
99
struct Circle {
@@ -110,11 +121,13 @@ impl Circle {
110
121
}
111
122
```
112
123
113
- # Chaining method calls
124
+ <!-- # Chaining method calls -->
125
+ # メソッドチェーン
114
126
115
- So, now we know how to call a method, such as ` foo.bar() ` . But what about our
127
+ <!-- So, now we know how to call a method, such as `foo.bar()`. But what about our
116
128
original example, `foo.bar().baz()`? This is called ‘method chaining’. Let’s
117
- look at an example:
129
+ look at an example: -->
130
+ ここまでで、` foo.bar() ` というようなメソッドの呼び出し方が分かりましたね。ですが元の例の ` foo.bar().baz() ` はどうなっているのでしょう?これは「メソッドチェーン」と呼ばれています。以下の例を見て下さい。
118
131
119
132
``` rust
120
133
struct Circle {
@@ -142,7 +155,8 @@ fn main() {
142
155
}
143
156
```
144
157
145
- Check the return type:
158
+ <!-- Check the return type: -->
159
+ 以下の返す型を確認して下さい。
146
160
147
161
``` rust
148
162
# struct Circle ;
@@ -151,13 +165,16 @@ fn grow(&self, increment: f64) -> Circle {
151
165
# Circle } }
152
166
```
153
167
154
- We just say we’re returning a ` Circle ` . With this method, we can grow a new
155
- ` Circle ` to any arbitrary size.
168
+ <!-- We just say we’re returning a `Circle`. With this method, we can grow a new
169
+ `Circle` to any arbitrary size. -->
170
+ 単に ` Circle ` を返しているだけです。このメソッドにより、私たちは新しい ` Circle ` を任意の大きさに拡大することができます。
156
171
157
- # Associated functions
172
+ <!-- # Associated functions -->
173
+ # 関連関数
158
174
159
- You can also define associated functions that do not take a ` self ` parameter.
160
- Here’s a pattern that’s very common in Rust code:
175
+ <!-- You can also define associated functions that do not take a `self` parameter.
176
+ Here’s a pattern that’s very common in Rust code: -->
177
+ あなたは ` self ` を引数に取らない関連関数を定義することもできます。以下のパターンはRustのコードにおいて非常にありふれた物です。
161
178
162
179
``` rust
163
180
struct Circle {
@@ -181,18 +198,21 @@ fn main() {
181
198
}
182
199
```
183
200
184
- This ‘associated function’ builds a new ` Circle ` for us. Note that associated
201
+ <!-- This ‘associated function’ builds a new `Circle` for us. Note that associated
185
202
functions are called with the `Struct::function()` syntax, rather than the
186
203
`ref.method()` syntax. Some other languages call associated functions ‘static
187
- methods’.
204
+ methods’. -->
205
+ この「関連関数」(associated function)は新たに ` Circle ` を構築します。この関数は ` ref.method() ` ではなく、 ` Struct::function() ` という構文で呼び出されることに注意して下さい。幾つかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。
188
206
189
- # Builder Pattern
207
+ <!-- # Builder Pattern -->
208
+ # Builderパターン
190
209
191
- Let’s say that we want our users to be able to create ` Circle ` s, but we will
210
+ <!-- Let’s say that we want our users to be able to create `Circle`s, but we will
192
211
allow them to only set the properties they care about. Otherwise, the `x`
193
212
and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesn’t
194
213
have method overloading, named arguments, or variable arguments. We employ
195
- the builder pattern instead. It looks like this:
214
+ the builder pattern instead. It looks like this: -->
215
+ ユーザが ` Circle ` を作成できるようにしつつも、書き換えたいプロパティだけを設定すれば良いようにしたいとしましょう。もし指定が無ければ ` x ` と ` y ` が ` 0.0 ` 、 ` radius ` が ` 1.0 ` であるものとします。Rustはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がない代わりにBuilderパターンを採用しており、それは以下のようになります。
196
216
197
217
``` rust
198
218
struct Circle {
@@ -251,9 +271,10 @@ fn main() {
251
271
}
252
272
```
253
273
254
- What we’ve done here is make another ` struct ` , ` CircleBuilder ` . We’ve defined our
274
+ <!-- What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our
255
275
builder methods on it. We’ve also defined our `area()` method on `Circle`. We
256
276
also made one more method on `CircleBuilder`: `finalize()`. This method creates
257
277
our final `Circle` from the builder. Now, we’ve used the type system to enforce
258
278
our concerns: we can use the methods on `CircleBuilder` to constrain making
259
- ` Circle ` s in any way we choose.
279
+ `Circle`s in any way we choose. -->
280
+ ここではもう1つの ` struct ` である ` CircleBuilder ` を作成しています。その中にBuilderメソッドを定義しました。また ` Circle ` に ` area() ` メソッドを定義しました。 そして ` CircleBuilder ` にもう1つ ` finalize() ` というメソッドを作りました。このメソッドはBuilderから最終的な ` Circle ` を作成します。さて、先程の要求を実施するために型システムを使いました。 ` CircleBuilder ` のメソッドを好きなように組み合わせ、作る ` Circle ` への制約を与えることができます。
0 commit comments