Skip to content

Commit 7c1ac27

Browse files
committed
update patterns
1 parent fe75086 commit 7c1ac27

File tree

3 files changed

+89
-144
lines changed

3 files changed

+89
-144
lines changed

1.6/ja/book/patterns.md

Lines changed: 87 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
<!-- % Patterns -->
33

44
<!-- Patterns are quite common in Rust. We use them in [variable -->
5-
<!-- bindings][bindings], [match statements][match], and other places, too. Let’s go -->
5+
<!-- bindings][bindings], [match expressions][match], and other places, too. Let’s go -->
66
<!-- on a whirlwind tour of all of the things patterns can do!-->
77
パターンはRustにおいて極めて一般的です。
88
パターンは [変数束縛][bindings], [マッチ文][match] などで使われています。
9-
109
さあ、めくるめくパターンの旅を始めましょう!
1110

1211
[bindings]: variable-bindings.html
1312
[match]: match.html
1413

1514
<!-- A quick refresher: you can match against literals directly, and `_` acts as an
1615
‘any’ case: -->
17-
簡単な復習:リテラルに対しては直接マッチさせられます。また、 `_` は「任意の」ケースとして振る舞います。
16+
簡単な復習:リテラルに対しては直接マッチ出来ます。
17+
また、 `_` は「任意の」ケースとして振る舞います。
1818

1919
```rust
2020
let x = 1;
@@ -32,10 +32,12 @@ match x {
3232

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

3739
```rust
38-
let x = 'x';
40+
let x = 1;
3941
let c = 'c';
4042

4143
match c {
@@ -50,13 +52,18 @@ println!("x: {}", x)
5052

5153
```text
5254
x: c c: c
53-
x: x
55+
x: 1
5456
```
5557

5658
<!-- In other words, `x =>` matches the pattern and introduces a new binding named -->
57-
<!-- `x` that’s in scope for the match arm. Because we already have a binding named -->
58-
<!-- `x`, this new `x` shadows it. -->
59-
別の言い方をすると、 `x =>` はパターンへのマッチだけでなく、マッチの腕内で有効な `x` という名前の束縛を導入します。既に `x` は束縛されていたので、この新しい `x` はそれを覆い隠します。
59+
<!-- `x`. This new binding is in scope for the match arm and takes on the value of -->
60+
<!-- `c`. Notice that the value of `x` outside the scope of the match has no bearing -->
61+
<!-- on the value of `x` within it. Because we already have a binding named `x`, this -->
62+
<!-- new `x` shadows it. -->
63+
別の言い方をすると、 `x =>` はパターンへにマッチし `x` という名前の束縛を導入します。
64+
この束縛はマッチの腕で有効で、 `c` の値を引き受けます。
65+
マッチのスコープ外にある `x` は内部での `x` の値に関係していないことに注意して下さい。
66+
既に `x` は束縛されていたので、この新しい `x` はそれを覆い隠します。
6067

6168
<!-- # Multiple patterns -->
6269
# 複式パターン
@@ -83,7 +90,7 @@ match x {
8390

8491
<!-- If you have a compound data type, like a [`struct`][struct], you can destructure it -->
8592
<!-- inside of a pattern: -->
86-
例えば [`struct`][struct] のような複合データ型を作成したいとき、パターン内でデータを分解することができます。
93+
例えば [`struct`][struct] のような複合データ型があるとき、パターン内でデータを分解することができます。
8794

8895
```rust
8996
struct Point {
@@ -135,8 +142,8 @@ match origin {
135142
<!-- This prints `x is 0`. -->
136143
これは `x is 0` を出力します。
137144

138-
<!-- You can do this kind of match on any member, not just the first:-->
139-
どのメンバに対してもこの種のマッチを行うことができます。たとえ最初ではなくても:
145+
<!-- You can do this kind of match on any member, not only the first: -->
146+
最初のものだけでなく、どのメンバに対してもこの種のマッチを行うことができます。
140147

141148
```rust
142149
struct Point {
@@ -154,8 +161,8 @@ match origin {
154161
<!-- This prints `y is 0`. -->
155162
これは `y is 0` を出力します。
156163

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

161168
[tuples]: primitive-types.html#tuples
@@ -178,10 +185,10 @@ match some_value {
178185
```
179186

180187
<!-- In the first arm, we bind the value inside the `Ok` variant to `value`. But -->
181-
<!-- in the `Err` arm, we use `_` to disregard the specific error, and just print -->
188+
<!-- in the `Err` arm, we use `_` to disregard the specific error, and print -->
182189
<!-- a general error message. -->
183-
最初の部分では `Ok` ヴァリアント内の値を `value` に結びつけています
184-
しかし `Err` 部分では、特定のエラーを避けて、標準的なエラーメッセージを表示するために `_` を使っています
190+
最初の部分では `Ok` ヴァリアント内の値を `value` に束縛しています
191+
しかし `Err` 部分では、`_` を使って特定のエラーを無視し、一般的なエラーメッセージを表示しています
185192

186193
<!-- `_` is valid in any pattern that creates a binding. This can be useful to -->
187194
<!-- ignore parts of a larger structure: -->
@@ -198,12 +205,50 @@ fn coordinate() -> (i32, i32, i32) {
198205
let (x, _, z) = coordinate();
199206
```
200207

201-
<!-- Here, we bind the first and last element of the tuple to `x` and `z`, but
202-
ignore the middle element. -->
208+
<!-- Here, we bind the first and last element of the tuple to `x` and `z`, but -->
209+
<!-- ignore the middle element. -->
203210
ここでは、タプルの最初と最後の要素を `x``z` に結びつけています。
204211

205-
<!-- Similarly, you can use `..` in a pattern to disregard multiple values. -->
206-
同様に `..` でパターン内の複数の値を無視することができます。
212+
<!-- It’s worth noting that using `_` never binds the value in the first place, -->
213+
<!-- which means a value may not move: -->
214+
`_` はそもそも値を束縛しない、つまり値がムーブしないということは特筆に値します。
215+
216+
```rust
217+
let tuple: (u32, String) = (5, String::from("five"));
218+
219+
# // Here, tuple is moved, because the String moved:
220+
// これだとタプルはムーブします。何故ならStringがムーブしているからです:
221+
let (x, _s) = tuple;
222+
223+
# // The next line would give "error: use of partially moved value: `tuple`"
224+
// この行は「error: use of partially moved value: `tuple`」を出します。
225+
// println!("Tuple is: {:?}", tuple);
226+
227+
// However,
228+
229+
let tuple = (5, String::from("five"));
230+
231+
// Here, tuple is _not_ moved, as the String was never moved, and u32 is Copy:
232+
// これだとタプルはムーブ _されません_ 。何故ならStringはムーブされず、u32はCopyだからです:
233+
let (x, _) = tuple;
234+
235+
# // That means this works:
236+
// つまりこれは動きます:
237+
println!("Tuple is: {:?}", tuple);
238+
```
239+
240+
<!-- This also means that any temporary variables will be dropped at the end of the -->
241+
<!-- statement: -->
242+
また、束縛されていない値は文の終わりでドロップされるということでもあります。
243+
244+
```rust
245+
# // Here, the String created will be dropped immediately, as it’s not bound:
246+
// 生成されたStringは束縛されていないので即座にドロップされる
247+
let _ = String::from(" hello ").trim();
248+
```
249+
250+
<!-- You can also use `..` in a pattern to disregard multiple values: -->
251+
複数の値を捨てるのには `..` パターンが使えます。
207252

208253
```rust
209254
enum OptionalTuple {
@@ -236,16 +281,17 @@ match x {
236281
}
237282
```
238283

239-
<!--This prints `Got a reference to 5`. -->
284+
<!-- This prints `Got a reference to 5`. -->
240285
これは `Got a reference to 5` を出力します。
241286

242287
[ref]: references-and-borrowing.html
243288

244-
<!-- Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref`
245-
keyword _creates_ a reference, for use in the pattern. If you need a mutable
246-
reference, `ref mut` will work in the same way: -->
289+
<!-- Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref` -->
290+
<!-- keyword _creates_ a reference, for use in the pattern. If you need a mutable -->
291+
<!-- reference, `ref mut` will work in the same way: -->
247292
ここで `match` 内の `r``&i32` 型を持っています。
248-
言い換えると `ref` キーワードがリファレンスを _作ります_
293+
言い換えると `ref` キーワードがパターン内で使うリファレンスを _作ります_
294+
ミュータブルなリファレンスが必要なら、 `ref mut` が同じように動作します。
249295

250296
```rust
251297
let mut x = 5;
@@ -293,7 +339,7 @@ match x {
293339
# 束縛
294340

295341
<!-- You can bind values to names with `@`: -->
296-
`@` で値を名前と結びつけることができます
342+
`@` で値を名前に束縛することができます
297343

298344
```rust
299345
let x = 1;
@@ -304,8 +350,8 @@ match x {
304350
}
305351
```
306352

307-
<!-- This prints `got a range element 1`. This is useful when you want to
308-
do a complicated match of part of a data structure: -->
353+
<!-- This prints `got a range element 1`. This is useful when you want to -->
354+
<!-- do a complicated match of part of a data structure: -->
309355
これは `got a range element 1` を出力します。
310356
データ構造の一部に対する複雑なマッチが欲しいときに有用です:
311357

@@ -316,19 +362,19 @@ struct Person {
316362
}
317363

318364
let name = "Steve".to_string();
319-
let mut x: Option<Person> = Some(Person { name: Some(name) });
365+
let x: Option<Person> = Some(Person { name: Some(name) });
320366
match x {
321367
Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),
322368
_ => {}
323369
}
324370
```
325371

326-
<!--This prints `Some("Steve")`: we’ve bound the inner `name` to `a`.-->
372+
<!-- This prints `Some("Steve")`: we’ve bound the inner `name` to `a`.-->
327373
これは `Some("Steve")` を出力します。内側の `name``a` に結びつけます。
328374

329-
<!-- If you use `@` with `|`, you need to make sure the name is bound in each part
330-
of the pattern: -->
331-
もし `|``@` を使うときは、パターンのそれぞれの部分が名前と結びついているか確認する必要があります:
375+
<!-- If you use `@` with `|`, you need to make sure the name is bound in each part -->
376+
<!-- of the pattern: -->
377+
もし `|``@` を使うときは、必ずそれぞれのパターンが名前と結びついている必要があります。
332378

333379
```rust
334380
let x = 5;
@@ -342,7 +388,7 @@ match x {
342388
<!-- # Guards -->
343389
# ガード
344390

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

348394
```rust
@@ -360,10 +406,10 @@ match x {
360406
}
361407
```
362408

363-
<!--This prints `Got an int!`. -->
409+
<!-- This prints `Got an int!`. -->
364410
これは `Got an int!` を出力します。
365411

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

369415
```rust
@@ -376,8 +422,8 @@ match x {
376422
}
377423
```
378424

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

383429
```text
@@ -394,8 +440,8 @@ just the `5`. In other words, the precedence of `if` behaves like this: -->
394440
<!-- # Mix and Match -->
395441
# 混ぜてマッチ
396442

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

401447
```rust,ignore

TranslationTable.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| antipattern | アンチパターン
2121
| application | アプリケーション
2222
| arity | アリティ
23+
| (マッチの)arm | 腕
2324
| array | 配列
2425
| assignment | 代入
2526
| associated - | 関連-
@@ -44,6 +45,7 @@
4445
| command line | コマンドライン
4546
| compile-time error | コンパイル時エラー
4647
| compiler | コンパイラ
48+
| compound data type | 複合データ型
4749
| composable | 合成可能
4850
| computer science | コンピュータサイエンス
4951
| concurrency | 並行性

diff-1.6.0..1.9.0/src/doc/book/patterns.md

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,6 @@
11
--- a/src/doc/book/patterns.md
22
+++ b/src/doc/book/patterns.md
3-
@@ -1,7 +1,7 @@
4-
% Patterns
53

6-
Patterns are quite common in Rust. We use them in [variable
7-
-bindings][bindings], [match statements][match], and other places, too. Let’s go
8-
+bindings][bindings], [match expressions][match], and other places, too. Let’s go
9-
on a whirlwind tour of all of the things patterns can do!
10-
11-
[bindings]: variable-bindings.html
12-
@@ -27,7 +27,7 @@ There’s one pitfall with patterns: like anything that introduces a new binding
13-
they introduce shadowing. For example:
14-
15-
```rust
16-
-let x = 'x';
17-
+let x = 1;
18-
let c = 'c';
19-
20-
match c {
21-
@@ -41,12 +41,14 @@ This prints:
22-
23-
```text
24-
x: c c: c
25-
-x: x
26-
+x: 1
27-
```
28-
29-
In other words, `x =>` matches the pattern and introduces a new binding named
30-
-`x` that’s in scope for the match arm. Because we already have a binding named
31-
-`x`, this new `x` shadows it.
32-
+`x`. This new binding is in scope for the match arm and takes on the value of
33-
+`c`. Notice that the value of `x` outside the scope of the match has no bearing
34-
+on the value of `x` within it. Because we already have a binding named `x`, this
35-
+new `x` shadows it.
36-
37-
# Multiple patterns
38-
39-
@@ -116,7 +118,7 @@ match origin {
40-
41-
This prints `x is 0`.
42-
43-
-You can do this kind of match on any member, not just the first:
44-
+You can do this kind of match on any member, not only the first:
45-
46-
```rust
47-
struct Point {
48-
@@ -153,7 +155,7 @@ match some_value {
49-
```
50-
51-
In the first arm, we bind the value inside the `Ok` variant to `value`. But
52-
-in the `Err` arm, we use `_` to disregard the specific error, and just print
53-
+in the `Err` arm, we use `_` to disregard the specific error, and print
54-
a general error message.
55-
56-
`_` is valid in any pattern that creates a binding. This can be useful to
57-
@@ -171,7 +173,39 @@ let (x, _, z) = coordinate();
58-
Here, we bind the first and last element of the tuple to `x` and `z`, but
59-
ignore the middle element.
60-
61-
-Similarly, you can use `..` in a pattern to disregard multiple values.
62-
+It’s worth noting that using `_` never binds the value in the first place,
63-
+which means a value may not move:
64-
+
65-
+```rust
66-
+let tuple: (u32, String) = (5, String::from("five"));
67-
+
68-
+// Here, tuple is moved, because the String moved:
69-
+let (x, _s) = tuple;
70-
+
71-
+// The next line would give "error: use of partially moved value: `tuple`"
72-
+// println!("Tuple is: {:?}", tuple);
73-
+
74-
+// However,
75-
+
76-
+let tuple = (5, String::from("five"));
77-
+
78-
+// Here, tuple is _not_ moved, as the String was never moved, and u32 is Copy:
79-
+let (x, _) = tuple;
80-
+
81-
+// That means this works:
82-
+println!("Tuple is: {:?}", tuple);
83-
+```
84-
+
85-
+This also means that any temporary variables will be dropped at the end of the
86-
+statement:
87-
+
88-
+```rust
89-
+// Here, the String created will be dropped immediately, as it’s not bound:
90-
+
91-
+let _ = String::from(" hello ").trim();
92-
+```
93-
+
94-
+You can also use `..` in a pattern to disregard multiple values:
95-
96-
```rust
97-
enum OptionalTuple {
98-
@@ -269,7 +303,7 @@ struct Person {
99-
}
100-
101-
let name = "Steve".to_string();
102-
-let mut x: Option<Person> = Some(Person { name: Some(name) });
103-
+let x: Option<Person> = Some(Person { name: Some(name) });
104-
match x {
105-
Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),
106-
_ => {}
1074
@@ -324,7 +358,7 @@ match x {
1085
```
1096

0 commit comments

Comments
 (0)