11% パターン
22<!-- % Patterns -->
33
4- <!-- Patterns are quite common in Rust. -->
4+ <!-- Patterns are quite common in Rust. We use them in [variable -->
5+ <!-- bindings][bindings], [match expressions][match], and other places, too. Let’s go -->
6+ <!-- on a whirlwind tour of all of the things patterns can do!-->
57パターンはRustにおいて極めて一般的です。
6-
7- <!-- We use them in [variable
8- bindings][bindings], [match statements][match], and other places, too.-->
98パターンは [ 変数束縛] [ bindings ] , [ マッチ文] [ match ] などで使われています。
10-
11- <!-- Let’s go on a whirlwind tour of all of the things patterns can do!-->
129さあ、めくるめくパターンの旅を始めましょう!
1310
1411[ bindings ] : variable-bindings.html
1512[ match ] : match.html
1613
1714<!-- A quick refresher: you can match against literals directly, and `_` acts as an
1815‘any’ case: -->
19- 簡単な復習:リテラルに対しては直接マッチさせられます。また、 ` _ ` は「任意の」ケースとして振る舞います。
16+ 簡単な復習:リテラルに対しては直接マッチ出来ます。
17+ また、 ` _ ` は「任意の」ケースとして振る舞います。
2018
2119``` rust
2220let x = 1 ;
@@ -32,11 +30,14 @@ match x {
3230<!-- This prints `one`. -->
3331これは ` one ` を表示します。
3432
35- <!-- There’s one pitfall with patterns: like anything that introduces a new binding,they introduce shadowing. For example: -->
36- パターンには一つ落とし穴があります。新しい束縛を導入すると、他の束縛を導入するものと同じように、シャドーイングします。例えば:
33+ <!-- There’s one pitfall with patterns: like anything that introduces a new binding, -->
34+ <!-- they introduce shadowing. For example: -->
35+ パターンには一つ落とし穴があります。
36+ 新しい束縛を導入すると、他の束縛を導入するものと同じように、シャドーイングします。
37+ 例えば:
3738
3839``` rust
39- let x = 'x' ;
40+ let x = 1 ;
4041let c = 'c' ;
4142
4243match c {
@@ -51,13 +52,18 @@ println!("x: {}", x)
5152
5253``` text
5354x: c c: c
54- x: x
55+ x: 1
5556```
5657
57- <!-- In other words, `x =>` matches the pattern and introduces a new binding named
58- `x` that’s in scope for the match arm. Because we already have a binding named
59- `x`, this new `x` shadows it. -->
60- 別の言い方をすると、 ` x => ` はパターンへのマッチだけでなく、マッチの腕内で有効な ` x ` という名前の束縛を導入します。既に ` x ` は束縛されていたので、この新しい ` x ` はそれを覆い隠します。
58+ <!-- In other words, `x =>` matches the pattern and introduces a new binding named -->
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 ` はそれを覆い隠します。
6167
6268<!-- # Multiple patterns -->
6369# 複式パターン
@@ -82,9 +88,9 @@ match x {
8288<!-- # Destructuring -->
8389# デストラクチャリング
8490
85- <!-- If you have a compound data type, like a [`struct`][struct], you can destructure it
86- inside of a pattern: -->
87- 例えば [ ` struct ` ] [ struct ] のような複合データ型を作成したいとき 、パターン内でデータを分解することができます。
91+ <!-- If you have a compound data type, like a [`struct`][struct], you can destructure it -->
92+ <!-- inside of a pattern: -->
93+ 例えば [ ` struct ` ] [ struct ] のような複合データ型があるとき 、パターン内でデータを分解することができます。
8894
8995``` rust
9096struct Point {
@@ -136,8 +142,8 @@ match origin {
136142<!-- This prints `x is 0`. -->
137143これは ` x is 0 ` を出力します。
138144
139- <!-- You can do this kind of match on any member, not just the first:-->
140- どのメンバに対してもこの種のマッチを行うことができます。たとえ最初ではなくても:
145+ <!-- You can do this kind of match on any member, not only the first: -->
146+ 最初のものだけでなく、 どのメンバに対してもこの種のマッチを行うことができます。
141147
142148``` rust
143149struct Point {
@@ -155,8 +161,8 @@ match origin {
155161<!-- This prints `y is 0`. -->
156162これは ` y is 0 ` を出力します。
157163
158- <!-- This ‘destructuring’ behavior works on any compound data type, like
159- [tuples][tuples] or [enums][enums]. -->
164+ <!-- This ‘destructuring’ behavior works on any compound data type, like -->
165+ <!-- [tuples][tuples] or [enums][enums]. -->
160166この「デストラクチャリング(destructuring)」と呼ばれる振る舞いは、 [ タプル] [ tuples ] や [ 列挙型] [ enums ] のような、複合データ型で使用できます。
161167
162168[ tuples ] : primitive-types.html#tuples
@@ -166,9 +172,8 @@ match origin {
166172# 束縛の無視
167173
168174<!-- You can use `_` in a pattern to disregard the type and value.-->
169- パターン内の型や値を無視するために ` _ ` を使うことができます。
170-
171175<!-- For example, here’s a `match` against a `Result<T, E>`: -->
176+ パターン内の型や値を無視するために ` _ ` を使うことができます。
172177例として、 ` Result<T, E> ` に対して ` match ` を適用してみましょう:
173178
174179``` rust
@@ -179,14 +184,16 @@ match some_value {
179184}
180185```
181186
182- <!-- In the first arm, we bind the value inside the `Ok` variant to `value`. But
183- in the `Err` arm, we use `_` to disregard the specific error, and just print
184- a general error message. -->
185- 最初の部分では ` Ok ` ヴァリアント内の値を ` value ` に結びつけています。しかし ` Err ` 部分では、特定のエラーを避けて、標準的なエラーメッセージを表示するために ` _ ` を使っています。
187+ <!-- In the first arm, we bind the value inside the `Ok` variant to `value`. But -->
188+ <!-- in the `Err` arm, we use `_` to disregard the specific error, and print -->
189+ <!-- a general error message. -->
190+ 最初の部分では ` Ok ` ヴァリアント内の値を ` value ` に束縛しています。
191+ しかし ` Err ` 部分では、` _ ` を使って特定のエラーを無視し、一般的なエラーメッセージを表示しています。
186192
187- <!-- `_` is valid in any pattern that creates a binding. This can be useful to
188- ignore parts of a larger structure: -->
189- ` _ ` は束縛を伴うどんなパターンにおいても有効です。これは大きな構造の一部分を無視する際に有用です。
193+ <!-- `_` is valid in any pattern that creates a binding. This can be useful to -->
194+ <!-- ignore parts of a larger structure: -->
195+ ` _ ` は束縛を伴うどんなパターンにおいても有効です。
196+ これは大きな構造の一部分を無視する際に有用です。
190197
191198``` rust
192199fn coordinate () -> (i32 , i32 , i32 ) {
@@ -198,12 +205,51 @@ fn coordinate() -> (i32, i32, i32) {
198205let (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+
230+ let tuple = (5 , String :: from (" five" ));
231+
232+ # // Here, tuple is _not_ moved, as the String was never moved, and u32 is Copy:
233+ // これだとタプルはムーブ _されません_ 。何故ならStringはムーブされず、u32はCopyだからです:
234+ let (x , _ ) = tuple ;
235+
236+ # // That means this works:
237+ // つまりこれは動きます:
238+ println! (" Tuple is: {:?}" , tuple );
239+ ```
240+
241+ <!-- This also means that any temporary variables will be dropped at the end of the -->
242+ <!-- statement: -->
243+ また、束縛されていない値は文の終わりでドロップされるということでもあります。
244+
245+ ``` rust
246+ # // Here, the String created will be dropped immediately, as it’s not bound:
247+ // 生成されたStringは束縛されていないので即座にドロップされる
248+ let _ = String :: from (" hello " ). trim ();
249+ ```
250+
251+ <!-- You can also use `..` in a pattern to disregard multiple values: -->
252+ 複数の値を捨てるのには ` .. ` パターンが使えます。
207253
208254``` rust
209255enum OptionalTuple {
@@ -236,15 +282,17 @@ match x {
236282}
237283```
238284
239- <!-- This prints `Got a reference to 5`. -->
285+ <!-- This prints `Got a reference to 5`. -->
240286これは ` Got a reference to 5 ` を出力します。
241287
242288[ ref ] : references-and-borrowing.html
243289
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: -->
247- ここで ` match ` 内の ` r ` は ` &i32 ` 型を持っています。言い換えると ` ref ` キーワードがリファレンスを _ 作ります_ 。
290+ <!-- Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref` -->
291+ <!-- keyword _creates_ a reference, for use in the pattern. If you need a mutable -->
292+ <!-- reference, `ref mut` will work in the same way: -->
293+ ここで ` match ` 内の ` r ` は ` &i32 ` 型を持っています。
294+ 言い換えると ` ref ` キーワードがパターン内で使うリファレンスを _ 作ります_ 。
295+ ミュータブルなリファレンスが必要なら、 ` ref mut ` が同じように動作します。
248296
249297``` rust
250298let mut x = 5 ;
@@ -292,7 +340,7 @@ match x {
292340# 束縛
293341
294342<!-- You can bind values to names with `@`: -->
295- ` @ ` で値を名前と結びつけることができます 。
343+ ` @ ` で値を名前に束縛することができます 。
296344
297345``` rust
298346let x = 1 ;
@@ -303,8 +351,8 @@ match x {
303351}
304352```
305353
306- <!-- This prints `got a range element 1`. This is useful when you want to
307- do a complicated match of part of a data structure: -->
354+ <!-- This prints `got a range element 1`. This is useful when you want to -->
355+ <!-- do a complicated match of part of a data structure: -->
308356これは ` got a range element 1 ` を出力します。
309357データ構造の一部に対する複雑なマッチが欲しいときに有用です:
310358
@@ -315,19 +363,19 @@ struct Person {
315363}
316364
317365let name = " Steve" . to_string ();
318- let mut x : Option <Person > = Some (Person { name : Some (name ) });
366+ let x : Option <Person > = Some (Person { name : Some (name ) });
319367match x {
320368 Some (Person { name : ref a @ Some (_ ), .. }) => println! (" {:?}" , a ),
321369 _ => {}
322370}
323371```
324372
325- <!-- This prints `Some("Steve")`: we’ve bound the inner `name` to `a`.-->
373+ <!-- This prints `Some("Steve")`: we’ve bound the inner `name` to `a`.-->
326374これは ` Some("Steve") ` を出力します。内側の ` name ` を ` a ` に結びつけます。
327375
328- <!-- If you use `@` with `|`, you need to make sure the name is bound in each part
329- of the pattern: -->
330- もし ` | ` で ` @ ` を使うときは、パターンのそれぞれの部分が名前と結びついているか確認する必要があります:
376+ <!-- If you use `@` with `|`, you need to make sure the name is bound in each part -->
377+ <!-- of the pattern: -->
378+ もし ` | ` で ` @ ` を使うときは、必ずそれぞれのパターンが名前と結びついている必要があります。
331379
332380``` rust
333381let x = 5 ;
@@ -341,8 +389,8 @@ match x {
341389<!-- # Guards -->
342390# ガード
343391
344- <!-- You can introduce ‘match guards’ with `if`: -->
345- ` if ` を使うことでマッチガードを導入することができます :
392+ <!-- You can introduce ‘match guards’ with `if`: -->
393+ ` if ` を使うことで「マッチガード」を導入することができます :
346394
347395``` rust
348396enum OptionalInt {
@@ -359,10 +407,10 @@ match x {
359407}
360408```
361409
362- <!-- This prints `Got an int!`. -->
410+ <!-- This prints `Got an int!`. -->
363411これは ` Got an int! ` を出力します。
364412
365- <!-- If you’re using `if` with multiple patterns, the `if` applies to both sides:-->
413+ <!-- If you’re using `if` with multiple patterns, the `if` applies to both sides: -->
366414複式パターンで ` if ` を使うと、 ` if ` は両方に適用されます:
367415
368416``` rust
@@ -375,8 +423,8 @@ match x {
375423}
376424```
377425
378- <!-- This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to
379- just the `5`. In other words, the precedence of `if` behaves like this: -->
426+ <!-- This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to -->
427+ <!-- only the `5`. In other words, the precedence of `if` behaves like this: -->
380428これは ` no ` を出力します。なぜなら ` if ` は ` 4 | 5 ` 全体に適用されるのであって、 ` 5 ` 単独に対してではないからです。つまり ` if ` 節は以下のように振舞います:
381429
382430``` text
@@ -393,8 +441,8 @@ just the `5`. In other words, the precedence of `if` behaves like this: -->
393441<!-- # Mix and Match -->
394442# 混ぜてマッチ
395443
396- <!-- Whew! That’s a lot of different ways to match things, and they can all be
397- mixed and matched, depending on what you’re doing: -->
444+ <!-- Whew! That’s a lot of different ways to match things, and they can all be -->
445+ <!-- mixed and matched, depending on what you’re doing: -->
398446ふう、マッチには様々な方法があるのですね。やりたいこと次第で、それらを混ぜてマッチさせることもできます:
399447
400448``` rust,ignore
0 commit comments