Skip to content

Commit 547121c

Browse files
committed
Merge pull request #103 from tatsuya6502/patterns-v1.6
4.14. Patterns
2 parents 98cb1ac + a593bdd commit 547121c

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

1.6/ja/book/patterns.md

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
% パターン Patterns
2-
<!--
3-
% Patterns
4-
-->
5-
<!--Patterns are quite common in Rust. -->
6-
パターンはRustにおいて極めて一般的な方法です。
1+
% パターン
2+
<!-- % Patterns -->
3+
4+
<!-- Patterns are quite common in Rust. -->
5+
パターンはRustにおいて極めて一般的です。
6+
77
<!-- We use them in [variable
88
bindings][bindings], [match statements][match], and other places, too.-->
9-
パターンは、[変数束縛][bindings][マッチ文][match]、などで使われています。
9+
パターンは [変数束縛][bindings], [マッチ文][match] などで使われています。
10+
1011
<!--Let’s go on a whirlwind tour of all of the things patterns can do!-->
1112
さあ、めくるめくパターンの旅を始めましょう!
13+
1214
[bindings]: variable-bindings.html
1315
[match]: match.html
1416

1517
<!-- A quick refresher: you can match against literals directly, and `_` acts as an
1618
‘any’ case: -->
17-
簡単な復習: パターンはリテラルに対しては直接マッチさせることができます。また、 `_` は「any」型として振る舞います
19+
簡単な復習:リテラルに対しては直接マッチさせられます。また、 `_` は「任意の」ケースとして振る舞います
1820

1921
```rust
2022
let x = 1;
@@ -31,7 +33,7 @@ match x {
3133
これは `one` を表示します。
3234

3335
<!-- There’s one pitfall with patterns: like anything that introduces a new binding,they introduce shadowing. For example: -->
34-
パターンには一つ落とし穴があります。新しい束縛を導入すると、他の束縛を導入するものと同じように、シャドーイングをします。
36+
パターンには一つ落とし穴があります。新しい束縛を導入すると、他の束縛を導入するものと同じように、シャドーイングします。例えば:
3537

3638
```rust
3739
let x = 'x';
@@ -45,7 +47,7 @@ println!("x: {}", x)
4547
```
4648

4749
<!-- This prints:-->
48-
これの結果は以下のようになります
50+
これの結果は以下のようになります
4951

5052
```text
5153
x: c c: c
@@ -55,13 +57,13 @@ x: x
5557
<!-- In other words, `x =>` matches the pattern and introduces a new binding named
5658
`x` that’s in scope for the match arm. Because we already have a binding named
5759
`x`, this new `x` shadows it. -->
58-
説明すると`x =>` はパターンへのマッチだけでなく、マッチの腕内で有効な `x` という名前の束縛を導入します。なぜなら既に `x` は束縛されており、この新しい `x` はそれを覆い隠します。
60+
別の言い方をすると`x =>` はパターンへのマッチだけでなく、マッチの腕内で有効な `x` という名前の束縛を導入します。既に `x` は束縛されていたので、この新しい `x` はそれを覆い隠します。
5961

6062
<!-- # Multiple patterns -->
6163
# 複式パターン
6264

6365
<!-- You can match multiple patterns with `|`: -->
64-
`|` を使うと、複式パターンが導入出来ます。
66+
`|` を使うと、複式パターンが導入できます:
6567

6668

6769
```rust
@@ -78,11 +80,11 @@ match x {
7880
これは、 `one or two` を出力します。
7981

8082
<!-- # Destructuring -->
81-
# デストラクチャ
83+
# デストラクチャリング
8284

8385
<!-- If you have a compound data type, like a [`struct`][struct], you can destructure it
8486
inside of a pattern: -->
85-
例えば[`struct`][struct]のようなデータ型を作成したいとき、パターン内でデータを分配することが出来ます
87+
例えば [`struct`][struct] のような複合データ型を作成したいとき、パターン内でデータを分解することができます
8688

8789
```rust
8890
struct Point {
@@ -100,7 +102,7 @@ match origin {
100102
[struct]: structs.html
101103

102104
<!-- We can use `:` to give a value a different name.-->
103-
値に別の名前を付けたいときは、 `:` を使うことが出来ます
105+
値に別の名前を付けたいときは、 `:` を使うことができます
104106

105107
```rust
106108
struct Point {
@@ -116,7 +118,7 @@ match origin {
116118
```
117119

118120
<!-- If we only care about some of the values, we don’t have to give them all names: -->
119-
値のうちいくつかを扱いたい場合は、値の全てに名前を付ける必要はありません。
121+
値の一部だけを扱いたい場合は、値の全てに名前を付ける必要はありません。
120122

121123
```rust
122124
struct Point {
@@ -135,7 +137,7 @@ match origin {
135137
これは `x is 0` を出力します。
136138

137139
<!-- You can do this kind of match on any member, not just the first:-->
138-
どのメンバーに対してもこの種のマッチを行うことが出来ます。たとえ最初ではなくても
140+
どのメンバに対してもこの種のマッチを行うことができます。たとえ最初ではなくても
139141

140142
```rust
141143
struct Point {
@@ -155,8 +157,7 @@ match origin {
155157

156158
<!-- This ‘destructuring’ behavior works on any compound data type, like
157159
[tuples][tuples] or [enums][enums]. -->
158-
この「デストラクチャリング」と呼ばれる振る舞いは、[タプル][tuples][列挙型][enum]のような、構成されたデータ型で起こります。
159-
160+
この「デストラクチャリング(destructuring)」と呼ばれる振る舞いは、 [タプル][tuples][列挙型][enums] のような、複合データ型で使用できます。
160161

161162
[tuples]: primitive-types.html#tuples
162163
[enums]: enums.html
@@ -165,10 +166,10 @@ match origin {
165166
# 束縛の無視
166167

167168
<!-- You can use `_` in a pattern to disregard the type and value.-->
168-
パターン内の型や値を無視するために `_` を使うことが出来ます
169+
パターン内の型や値を無視するために `_` を使うことができます
169170

170171
<!-- For example, here’s a `match` against a `Result<T, E>`: -->
171-
例として、 `Result<T, E>` に対して `match` を適用してみましょう
172+
例として、 `Result<T, E>` に対して `match` を適用してみましょう
172173

173174
```rust
174175
# let some_value: Result<i32, &'static str> = Err("There was an error");
@@ -181,14 +182,16 @@ match some_value {
181182
<!-- In the first arm, we bind the value inside the `Ok` variant to `value`. But
182183
in the `Err` arm, we use `_` to disregard the specific error, and just print
183184
a general error message. -->
184-
最初の部分では、 `Ok` ヴァリアント内の値を `value` に結びつけています。しかし、 `Err` 部分ですと、特定のエラーを避けるために、また標準エラーメッセージを表示するために `_` を使っています。
185+
最初の部分では `Ok` ヴァリアント内の値を `value` に結びつけています。しかし `Err` 部分では、特定のエラーを避けて、標準的なエラーメッセージを表示するために `_` を使っています。
186+
185187
<!-- `_` is valid in any pattern that creates a binding. This can be useful to
186188
ignore parts of a larger structure: -->
187-
`_` は束縛を伴うどんなパターンに於いても有効です。これは大きな構造の一部分を無視する際に有用です。
189+
`_` は束縛を伴うどんなパターンにおいても有効です。これは大きな構造の一部分を無視する際に有用です。
188190

189191
```rust
190192
fn coordinate() -> (i32, i32, i32) {
191-
// generate and return some sort of triple tuple
193+
# // generate and return some sort of triple tuple
194+
// 3要素のタプルを生成して返す
192195
# (1, 2, 3)
193196
}
194197

@@ -198,8 +201,9 @@ let (x, _, z) = coordinate();
198201
<!-- Here, we bind the first and last element of the tuple to `x` and `z`, but
199202
ignore the middle element. -->
200203
ここでは、タプルの最初と最後の要素を `x``z` に結びつけています。
201-
<!-- Similarly, you can use `..` in a pattern to disregard multiple values. -->
202-
同様に、 `..` でパターン内の複数の値を無視することが出来ます。
204+
205+
<!-- Similarly, you can use `..` in a pattern to disregard multiple values. -->
206+
同様に `..` でパターン内の複数の値を無視することができます。
203207

204208
```rust
205209
enum OptionalTuple {
@@ -222,7 +226,7 @@ match x {
222226
# ref と ref mut
223227

224228
<!-- If you want to get a [reference][ref], use the `ref` keyword:-->
225-
もし[リファレンス][ref]を取得したいときは `ref` キーワードを使いましょう。
229+
もし [リファレンス][ref] を取得したいときは `ref` キーワードを使いましょう。
226230

227231
```rust
228232
let x = 5;
@@ -240,7 +244,7 @@ match x {
240244
<!-- Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref`
241245
keyword _creates_ a reference, for use in the pattern. If you need a mutable
242246
reference, `ref mut` will work in the same way: -->
243-
ここで `match` 内の `r``&i32` 型を持っています。言い換えると `ref` キーワードがリファレンスを _作ります_
247+
ここで `match` 内の `r``&i32` 型を持っています。言い換えると `ref` キーワードがリファレンスを _作ります_
244248

245249
```rust
246250
let mut x = 5;
@@ -254,7 +258,7 @@ match x {
254258
# レンジ
255259

256260
<!-- You can match a range of values with `...`: -->
257-
`...` で値のレンジのマッチを行うことが出来ます。
261+
`...` で値のレンジのマッチを行うことができます:
258262

259263
```rust
260264
let x = 1;
@@ -267,8 +271,9 @@ match x {
267271

268272
<!-- This prints `one through five`. -->
269273
これは `one through five` を出力します。
274+
270275
<!-- Ranges are mostly used with integers and `char`s: -->
271-
レンジは大体、整数か `char` 型で使われます
276+
レンジは大体、整数か `char` 型で使われます
272277

273278
```rust
274279
let x = '💅';
@@ -287,7 +292,7 @@ match x {
287292
# 束縛
288293

289294
<!-- You can bind values to names with `@`: -->
290-
`@` で値を名前と結びつけることが出来ます
295+
`@` で値を名前と結びつけることができます
291296

292297
```rust
293298
let x = 1;
@@ -301,7 +306,7 @@ match x {
301306
<!-- This prints `got a range element 1`. This is useful when you want to
302307
do a complicated match of part of a data structure: -->
303308
これは `got a range element 1` を出力します。
304-
データ構造の一部に対する複雑なマッチが欲しいときに有用です
309+
データ構造の一部に対する複雑なマッチが欲しいときに有用です
305310

306311
```rust
307312
#[derive(Debug)]
@@ -319,10 +324,10 @@ match x {
319324

320325
<!--This prints `Some("Steve")`: we’ve bound the inner `name` to `a`.-->
321326
これは `Some("Steve")` を出力します。内側の `name``a` に結びつけます。
327+
322328
<!-- If you use `@` with `|`, you need to make sure the name is bound in each part
323329
of the pattern: -->
324-
もし `|``@` を使うときは、パターンのそれぞれの部分が名前と結びついているか確認する必要があります。
325-
330+
もし `|``@` を使うときは、パターンのそれぞれの部分が名前と結びついているか確認する必要があります:
326331

327332
```rust
328333
let x = 5;
@@ -337,7 +342,7 @@ match x {
337342
# ガード
338343

339344
<!--You can introduce ‘match guards’ with `if`: -->
340-
`if` を使うことでマッチガードを導入することが出来ます。
345+
`if` を使うことでマッチガードを導入することができます:
341346

342347
```rust
343348
enum OptionalInt {
@@ -358,7 +363,7 @@ match x {
358363
これは `Got an int!` を出力します。
359364

360365
<!--If you’re using `if` with multiple patterns, the `if` applies to both sides:-->
361-
複式パターンで `if` を使うと、 `if` は両方に適用されます
366+
複式パターンで `if` を使うと、 `if` は両方に適用されます
362367

363368
```rust
364369
let x = 4;
@@ -372,14 +377,14 @@ match x {
372377

373378
<!--This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to
374379
just the `5`. In other words, the precedence of `if` behaves like this: -->
375-
これは `no` を出力します。なぜなら `if``4 | 5` 全体に適用されるのであって、 `5` 単独に対してではないからです。つまり `if` 節は以下のように振舞います
380+
これは `no` を出力します。なぜなら `if``4 | 5` 全体に適用されるのであって、 `5` 単独に対してではないからです。つまり `if` 節は以下のように振舞います
376381

377382
```text
378383
(4 | 5) if y => ...
379384
```
380385

381386
<!--not this: -->
382-
次のようには解釈されません
387+
次のようには解釈されません
383388

384389
```text
385390
4 | (5 if y) => ...
@@ -390,7 +395,7 @@ just the `5`. In other words, the precedence of `if` behaves like this: -->
390395

391396
<!--Whew! That’s a lot of different ways to match things, and they can all be
392397
mixed and matched, depending on what you’re doing: -->
393-
さて、マッチにはまだ沢山の方法があります。やろうとしていることに依りますが、それらの方法を混ぜてマッチさせることも出来ます。
398+
ふう、マッチには様々な方法があるのですね。やりたいこと次第で、それらを混ぜてマッチさせることもできます:
394399

395400
```rust,ignore
396401
match x {

0 commit comments

Comments
 (0)