Skip to content

Commit 258366d

Browse files
authored
Merge pull request #251 from tatsuya6502/method-syntax-pre1.9
Markdown のフォーマット修正:Method Syntax (1.9)
2 parents 12fe10a + b333d1f commit 258366d

File tree

1 file changed

+82
-58
lines changed

1 file changed

+82
-58
lines changed

1.9/ja/book/method-syntax.md

Lines changed: 82 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
% メソッド構文
22
<!-- % Method Syntax -->
33

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-
関数は素晴らしいのですが、幾つかのデータに対し複数の関数をまとめて呼び出したい時、困ったことになります。以下のコードについて考えてみます。
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+
関数は素晴らしいのですが、幾つかのデータに対し複数の関数をまとめて呼び出したい時、困ったことになります。
7+
以下のコードについて考えてみます。
78

89
```rust,ignore
910
baz(bar(foo));
1011
```
1112

12-
<!-- We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the
13-
order that the functions would get called in, that’s inside-out: ‘foo bar baz’.
14-
Wouldn’t it be nice if we could do this instead? -->
15-
私たちはこれを左から右へ、「baz bar foo」と読むことになりますが、関数が呼び出される順番は異なり、内側から外へ「foo bar baz」となります。もし代わりにこうできたらいいとは思いませんか?
13+
<!-- We would read this left-to-right, and so we see ‘baz bar foo’. But this isn’t the -->
14+
<!-- order that the functions would get called in, that’s inside-out: ‘foo bar baz’. -->
15+
<!-- Wouldn’t it be nice if we could do this instead? -->
16+
私たちはこれを左から右へ、「baz bar foo」と読むことになりますが、関数が呼び出される順番は異なり、内側から外へ「foo bar baz」となります。
17+
もし代わりにこうできたらいいとは思いませんか?
1618

1719
```rust,ignore
1820
foo.bar().baz();
1921
```
2022

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` キーワードによってこの「メソッド呼び出し構文」の機能を提供しています。
23+
<!-- Luckily, as you may have guessed with the leading question, you can! Rust provides -->
24+
<!-- the ability to use this ‘method call syntax’ via the `impl` keyword. -->
25+
最初の質問でもう分かっているかもしれませんが、幸いにもこれは可能です!
26+
Rustは `impl` キーワードによってこの「メソッド呼び出し構文」の機能を提供しています。
2427

2528
<!-- # Method calls -->
2629
# メソッド呼び出し
@@ -50,23 +53,28 @@ fn main() {
5053
<!-- This will print `12.566371`. -->
5154
これは `12.566371` と出力します。
5255

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` というメソッドを定義しています。
56-
57-
<!-- Methods take a special first parameter, of which there are three variants:
58-
`self`, `&self`, and `&mut self`. You can think of this first parameter as
59-
being the `foo` in `foo.bar()`. The three variants correspond to the three
60-
kinds of things `foo` could be: `self` if it’s just a value on the stack,
61-
`&self` if it’s a reference, and `&mut self` if it’s a mutable reference.
62-
Because we took the `&self` parameter to `area`, we can use it just like any
63-
other parameter. Because we know it’s a `Circle`, we can access the `radius`
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` へアクセスできます。
66-
67-
<!-- We should default to using `&self`, as you should prefer borrowing over taking
68-
ownership, as well as taking immutable references over mutable ones. Here’s an
69-
example of all three variants: -->
56+
<!-- We’ve made a `struct` that represents a circle. We then write an `impl` block, -->
57+
<!-- and inside it, define a method, `area`. -->
58+
私たちは円を表す `struct` を作りました。
59+
その際 `impl` ブロックを書き、その中に `area` というメソッドを定義しています。
60+
61+
<!-- Methods take a special first parameter, of which there are three variants: -->
62+
<!-- `self`, `&self`, and `&mut self`. You can think of this first parameter as -->
63+
<!-- being the `foo` in `foo.bar()`. The three variants correspond to the three -->
64+
<!-- kinds of things `foo` could be: `self` if it’s just a value on the stack, -->
65+
<!-- `&self` if it’s a reference, and `&mut self` if it’s a mutable reference. -->
66+
<!-- Because we took the `&self` parameter to `area`, we can use it just like any -->
67+
<!-- other parameter. Because we know it’s a `Circle`, we can access the `radius` -->
68+
<!-- just like we would with any other `struct`. -->
69+
メソッドに渡す特別な第1引数として、 `self``&self``&mut self` という3つの変形があります。
70+
第一引数は `foo.bar()` に於ける `foo` だと考えて下さい。
71+
3つの変形は `foo` が成り得る3種類の状態に対応しており、それぞれ `self` がスタック上の値である場合、 `&self` が参照である場合、 `&mut self` がミュータブルな参照である場合となっています。
72+
`area` では `&self` を受け取っているため、他の引数と同じように扱えます。
73+
引数が `Circle` であるのは分かっていますから、他の `struct` でするように `radius` へアクセスできます。
74+
75+
<!-- We should default to using `&self`, as you should prefer borrowing over taking -->
76+
<!-- ownership, as well as taking immutable references over mutable ones. Here’s an -->
77+
<!-- example of all three variants: -->
7078
所有権を渡すよりも借用を好んで使うべきなのは勿論のこと、ミュータブルな参照よりもイミュータブルな参照を渡すべきですから、 `&self` を常用すべきです。以下が3種類全ての例です。
7179

7280
```rust
@@ -91,9 +99,10 @@ impl Circle {
9199
}
92100
```
93101

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` ブロックを使用することができます。前述の例は以下のように書くこともできるでしょう。
102+
<!--You can use as many `impl` blocks as you’d like. The previous example could -->
103+
<!-- have also been written like this: -->
104+
好きな数だけ `impl` ブロックを使用することができます。
105+
前述の例は以下のように書くこともできるでしょう。
97106

98107
```rust
99108
struct Circle {
@@ -124,10 +133,13 @@ impl Circle {
124133
<!-- # Chaining method calls -->
125134
# メソッドチェーン
126135

127-
<!-- So, now we know how to call a method, such as `foo.bar()`. But what about our
128-
original example, `foo.bar().baz()`? This is called ‘method chaining’. Let’s
129-
look at an example: -->
130-
ここまでで、`foo.bar()` というようなメソッドの呼び出し方が分かりましたね。ですが元の例の `foo.bar().baz()` はどうなっているのでしょう?これは「メソッドチェーン」と呼ばれています。以下の例を見て下さい。
136+
<!-- So, now we know how to call a method, such as `foo.bar()`. But what about our -->
137+
<!-- original example, `foo.bar().baz()`? This is called ‘method chaining’. Let’s -->
138+
<!-- look at an example: -->
139+
ここまでで、`foo.bar()` というようなメソッドの呼び出し方が分かりましたね。
140+
ですが元の例の `foo.bar().baz()` はどうなっているのでしょう?
141+
これは「メソッドチェーン」と呼ばれています。
142+
以下の例を見て下さい。
131143

132144
```rust
133145
struct Circle {
@@ -165,16 +177,18 @@ fn grow(&self, increment: f64) -> Circle {
165177
# Circle } }
166178
```
167179

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` を任意の大きさに拡大することができます。
180+
<!-- We just say we’re returning a `Circle`. With this method, we can grow a new -->
181+
<!-- `Circle` to any arbitrary size. -->
182+
単に `Circle` を返しているだけです。
183+
このメソッドにより、私たちは新しい `Circle` を任意の大きさに拡大することができます。
171184

172185
<!-- # Associated functions -->
173186
# 関連関数
174187

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のコードにおいて非常にありふれた物です。
188+
<!-- You can also define associated functions that do not take a `self` parameter. -->
189+
<!-- Here’s a pattern that’s very common in Rust code: -->
190+
あなたは `self` を引数に取らない関連関数を定義することもできます。
191+
以下のパターンはRustのコードにおいて非常にありふれた物です。
178192

179193
```rust
180194
struct Circle {
@@ -198,21 +212,25 @@ fn main() {
198212
}
199213
```
200214

201-
<!-- This ‘associated function’ builds a new `Circle` for us. Note that associated
202-
functions are called with the `Struct::function()` syntax, rather than the
203-
`ref.method()` syntax. Some other languages call associated functions ‘static
204-
methods’. -->
205-
この「関連関数」(associated function)は新たに `Circle` を構築します。この関数は `ref.method()` ではなく、 `Struct::function()` という構文で呼び出されることに注意して下さい。幾つかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。
215+
<!-- This ‘associated function’ builds a new `Circle` for us. Note that associated -->
216+
<!-- functions are called with the `Struct::function()` syntax, rather than the -->
217+
<!-- `ref.method()` syntax. Some other languages call associated functions ‘static -->
218+
<!-- methods’. -->
219+
この「関連関数」(associated function)は新たに `Circle` を構築します。
220+
この関数は `ref.method()` ではなく、 `Struct::function()` という構文で呼び出されることに注意して下さい。
221+
幾つかの言語では、関連関数を「静的メソッド」(static methods)と呼んでいます。
206222

207223
<!-- # Builder Pattern -->
208224
# Builderパターン
209225

210-
<!-- Let’s say that we want our users to be able to create `Circle`s, but we will
211-
allow them to only set the properties they care about. Otherwise, the `x`
212-
and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesn’t
213-
have method overloading, named arguments, or variable arguments. We employ
214-
the builder pattern instead. It looks like this: -->
215-
ユーザが `Circle` を作成できるようにしつつも、書き換えたいプロパティだけを設定すれば良いようにしたいとしましょう。もし指定が無ければ `x``y``0.0``radius``1.0` であるものとします。Rustはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がない代わりにBuilderパターンを採用しており、それは以下のようになります。
226+
<!-- Let’s say that we want our users to be able to create `Circle`s, but we will -->
227+
<!-- allow them to only set the properties they care about. Otherwise, the `x` -->
228+
<!-- and `y` attributes will be `0.0`, and the `radius` will be `1.0`. Rust doesn’t -->
229+
<!-- have method overloading, named arguments, or variable arguments. We employ -->
230+
<!-- the builder pattern instead. It looks like this: -->
231+
ユーザが `Circle` を作成できるようにしつつも、書き換えたいプロパティだけを設定すれば良いようにしたいとしましょう。
232+
もし指定が無ければ `x``y``0.0``radius``1.0` であるものとします。
233+
Rustはメソッドのオーバーロードや名前付き引数、可変個引数といった機能がない代わりにBuilderパターンを採用しており、それは以下のようになります。
216234

217235
```rust
218236
struct Circle {
@@ -271,10 +289,16 @@ fn main() {
271289
}
272290
```
273291

274-
<!-- What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our
275-
builder methods on it. We’ve also defined our `area()` method on `Circle`. We
276-
also made one more method on `CircleBuilder`: `finalize()`. This method creates
277-
our final `Circle` from the builder. Now, we’ve used the type system to enforce
278-
our concerns: we can use the methods on `CircleBuilder` to constrain making
279-
`Circle`s in any way we choose. -->
280-
ここではもう1つの `struct` である `CircleBuilder` を作成しています。その中にBuilderメソッドを定義しました。また `Circle``area()` メソッドを定義しました。 そして `CircleBuilder` にもう1つ `finalize()` というメソッドを作りました。このメソッドはBuilderから最終的な `Circle` を作成します。さて、先程の要求を実施するために型システムを使いました。 `CircleBuilder` のメソッドを好きなように組み合わせ、作る `Circle` への制約を与えることができます。
292+
<!-- What we’ve done here is make another `struct`, `CircleBuilder`. We’ve defined our -->
293+
<!-- builder methods on it. We’ve also defined our `area()` method on `Circle`. We -->
294+
<!-- also made one more method on `CircleBuilder`: `finalize()`. This method creates -->
295+
<!-- our final `Circle` from the builder. Now, we’ve used the type system to enforce -->
296+
<!-- our concerns: we can use the methods on `CircleBuilder` to constrain making -->
297+
<!-- `Circle`s in any way we choose. -->
298+
ここではもう1つの `struct` である `CircleBuilder` を作成しています。
299+
その中にBuilderメソッドを定義しました。
300+
また `Circle``area()` メソッドを定義しました。
301+
そして `CircleBuilder` にもう1つ `finalize()` というメソッドを作りました。
302+
このメソッドはBuilderから最終的な `Circle` を作成します。
303+
さて、先程の要求を実施するために型システムを使いました。
304+
`CircleBuilder` のメソッドを好きなように組み合わせ、作る `Circle` への制約を与えることができます。

0 commit comments

Comments
 (0)