-
Notifications
You must be signed in to change notification settings - Fork 72
4.14. patterns.md #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9b4007c
8691eac
ca91987
7d2916c
0bb5a86
c97e1d9
6479124
088b393
97b875d
bb67563
7941eae
c932292
e792090
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,20 @@ | ||
| % Patterns | ||
|
|
||
| Patterns are quite common in Rust. We use them in [variable | ||
| bindings][bindings], [match statements][match], and other places, too. Let’s go | ||
| on a whirlwind tour of all of the things patterns can do! | ||
|
|
||
| % パターン Patterns | ||
| <!-- | ||
| % Patterns | ||
| --> | ||
| <!--Patterns are quite common in Rust. --> | ||
| パターンはRustにおいて極めて一般的な方法です。 | ||
| <!-- We use them in [variable | ||
| bindings][bindings], [match statements][match], and other places, too.--> | ||
| パターンは、[変数束縛][bindings]、[マッチ文][match]、などで使われています。 | ||
| <!--Let’s go on a whirlwind tour of all of the things patterns can do!--> | ||
| さあ、めくるめくパターンの旅を始めましょう! | ||
| [bindings]: variable-bindings.html | ||
| [match]: match.html | ||
|
|
||
| A quick refresher: you can match against literals directly, and `_` acts as an | ||
| ‘any’ case: | ||
| <!-- A quick refresher: you can match against literals directly, and `_` acts as an | ||
| ‘any’ case: --> | ||
| 簡単な復習: パターンはリテラルに対しては直接マッチさせることができます。また、 `_` は「any」型として振る舞います。 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 原文の表現が分かりづらいのかもしれませんが、you can match against literals directly, and
ということを言っています。 |
||
|
|
||
| ```rust | ||
| let x = 1; | ||
|
|
@@ -21,10 +27,11 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `one`. | ||
| <!-- This prints `one`. --> | ||
| これは `one` を表示します。 | ||
|
|
||
| There’s one pitfall with patterns: like anything that introduces a new binding, | ||
| they introduce shadowing. For example: | ||
| <!-- There’s one pitfall with patterns: like anything that introduces a new binding,they introduce shadowing. For example: --> | ||
| パターンには一つ落とし穴があります。新しい束縛を導入すると、他の束縛を導入するものと同じように、シャドーイングをします。 | ||
|
|
||
| ```rust | ||
| let x = 'x'; | ||
|
|
@@ -37,20 +44,25 @@ match c { | |
| println!("x: {}", x) | ||
| ``` | ||
|
|
||
| This prints: | ||
| <!-- This prints:--> | ||
| これの結果は以下のようになります。 | ||
|
|
||
| ```text | ||
| x: c c: c | ||
| x: x | ||
| ``` | ||
|
|
||
| In other words, `x =>` matches the pattern and introduces a new binding named | ||
| <!-- In other words, `x =>` matches the pattern and introduces a new binding named | ||
| `x` that’s in scope for the match arm. Because we already have a binding named | ||
| `x`, this new `x` shadows it. | ||
| `x`, this new `x` shadows it. --> | ||
| 説明すると、 `x =>` はパターンへのマッチだけでなく、マッチの腕内で有効な `x` という名前の束縛を導入します。なぜなら既に `x` は束縛されており、この新しい `x` はそれを覆い隠します。 | ||
|
|
||
| # Multiple patterns | ||
| <!-- # Multiple patterns --> | ||
| # 複式パターン | ||
|
|
||
| <!-- You can match multiple patterns with `|`: --> | ||
| `|` を使うと、複式パターンが導入出来ます。 | ||
|
|
||
| You can match multiple patterns with `|`: | ||
|
|
||
| ```rust | ||
| let x = 1; | ||
|
|
@@ -62,12 +74,15 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `one or two`. | ||
| <!--This prints `one or two`.--> | ||
| これは、 `one or two` を出力します。 | ||
|
|
||
| # Destructuring | ||
| <!-- # Destructuring --> | ||
| # デストラクチャ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [対訳] |
||
|
|
||
| If you have a compound data type, like a [`struct`][struct], you can destructure it | ||
| inside of a pattern: | ||
| <!-- If you have a compound data type, like a [`struct`][struct], you can destructure it | ||
| inside of a pattern: --> | ||
| 例えば、[`struct`][struct]のようなデータ型を作成したいとき、パターン内でデータを分配することが出来ます。 | ||
|
|
||
| ```rust | ||
| struct Point { | ||
|
|
@@ -84,7 +99,8 @@ match origin { | |
|
|
||
| [struct]: structs.html | ||
|
|
||
| We can use `:` to give a value a different name. | ||
| <!-- We can use `:` to give a value a different name.--> | ||
| 値に別の名前を付けたいときは、 `:` を使うことが出来ます。 | ||
|
|
||
| ```rust | ||
| struct Point { | ||
|
|
@@ -99,7 +115,8 @@ match origin { | |
| } | ||
| ``` | ||
|
|
||
| If we only care about some of the values, we don’t have to give them all names: | ||
| <!-- If we only care about some of the values, we don’t have to give them all names: --> | ||
| 値のうちいくつかを扱いたい場合は、値の全てに名前を付ける必要はありません。 | ||
|
|
||
| ```rust | ||
| struct Point { | ||
|
|
@@ -114,9 +131,11 @@ match origin { | |
| } | ||
| ``` | ||
|
|
||
| This prints `x is 0`. | ||
| <!-- This prints `x is 0`. --> | ||
| これは `x is 0` を出力します。 | ||
|
|
||
| You can do this kind of match on any member, not just the first: | ||
| <!-- You can do this kind of match on any member, not just the first:--> | ||
| どのメンバーに対してもこの種のマッチを行うことが出来ます。たとえ最初ではなくても。 | ||
|
|
||
| ```rust | ||
| struct Point { | ||
|
|
@@ -131,18 +150,25 @@ match origin { | |
| } | ||
| ``` | ||
|
|
||
| This prints `y is 0`. | ||
| <!-- This prints `y is 0`. --> | ||
| これは `y is 0` を出力します。 | ||
|
|
||
| <!-- This ‘destructuring’ behavior works on any compound data type, like | ||
| [tuples][tuples] or [enums][enums]. --> | ||
| この「デストラクチャリング」と呼ばれる振る舞いは、[タプル][tuples]や[列挙型][enum]のような、構成されたデータ型で起こります。 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [対訳]
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
「起こります」はややぎこちないので「動作します」はどうでしょうか。 |
||
|
|
||
| This ‘destructuring’ behavior works on any compound data type, like | ||
| [tuples][tuples] or [enums][enums]. | ||
|
|
||
| [tuples]: primitive-types.html#tuples | ||
| [enums]: enums.html | ||
|
|
||
| # Ignoring bindings | ||
| <!-- # Ignoring bindings --> | ||
| # 束縛の無視 | ||
|
|
||
| You can use `_` in a pattern to disregard the type and value. | ||
| For example, here’s a `match` against a `Result<T, E>`: | ||
| <!-- You can use `_` in a pattern to disregard the type and value.--> | ||
| パターン内の型や値を無視するために `_` を使うことが出来ます。 | ||
|
|
||
| <!-- For example, here’s a `match` against a `Result<T, E>`: --> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1行余計な空白が入っているようです。 |
||
| 例として、 `Result<T, E>` に対して `match` を適用してみましょう。 | ||
|
|
||
| ```rust | ||
| # let some_value: Result<i32, &'static str> = Err("There was an error"); | ||
|
|
@@ -152,12 +178,13 @@ match some_value { | |
| } | ||
| ``` | ||
|
|
||
| In the first arm, we bind the value inside the `Ok` variant to `value`. But | ||
| <!-- In the first arm, we bind the value inside the `Ok` variant to `value`. But | ||
| in the `Err` arm, we use `_` to disregard the specific error, and just print | ||
| a general error message. | ||
|
|
||
| `_` is valid in any pattern that creates a binding. This can be useful to | ||
| ignore parts of a larger structure: | ||
| a general error message. --> | ||
| 最初の部分では、 `Ok` ヴァリアント内の値を `value` に結びつけています。しかし、 `Err` 部分ですと、特定のエラーを避けるために、また標準エラーメッセージを表示するために `_` を使っています。 | ||
| <!-- `_` is valid in any pattern that creates a binding. This can be useful to | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 空行が1つ消えてしまっているようです。 |
||
| ignore parts of a larger structure: --> | ||
| `_` は束縛を伴うどんなパターンに於いても有効です。これは大きな構造の一部分を無視する際に有用です。 | ||
|
|
||
| ```rust | ||
| fn coordinate() -> (i32, i32, i32) { | ||
|
|
@@ -168,10 +195,11 @@ fn coordinate() -> (i32, i32, i32) { | |
| let (x, _, z) = coordinate(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. こちらのコメントの翻訳もお願いします。 |
||
| ``` | ||
|
|
||
| Here, we bind the first and last element of the tuple to `x` and `z`, but | ||
| ignore the middle element. | ||
|
|
||
| Similarly, you can use `..` in a pattern to disregard multiple values. | ||
| <!-- Here, we bind the first and last element of the tuple to `x` and `z`, but | ||
| ignore the middle element. --> | ||
| ここでは、タプルの最初と最後の要素を `x` と `z` に結びつけています。 | ||
| <!-- Similarly, you can use `..` in a pattern to disregard multiple values. --> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 空行が1行消えてしまっているようです。 |
||
| 同様に、 `..` でパターン内の複数の値を無視することが出来ます。 | ||
|
|
||
| ```rust | ||
| enum OptionalTuple { | ||
|
|
@@ -187,11 +215,14 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `Got a tuple!`. | ||
| <!--This prints `Got a tuple!`. --> | ||
| これは `Got a tuple!` を出力します。 | ||
|
|
||
| # ref and ref mut | ||
| <!-- # ref and ref mut --> | ||
| # ref と ref mut | ||
|
|
||
| If you want to get a [reference][ref], use the `ref` keyword: | ||
| <!-- If you want to get a [reference][ref], use the `ref` keyword:--> | ||
| もし[リファレンス][ref]を取得したいときは、 `ref` キーワードを使いましょう。 | ||
|
|
||
| ```rust | ||
| let x = 5; | ||
|
|
@@ -201,13 +232,15 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `Got a reference to 5`. | ||
| <!--This prints `Got a reference to 5`. --> | ||
| これは `Got a reference to 5` を出力します。 | ||
|
|
||
| [ref]: references-and-borrowing.html | ||
|
|
||
| Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref` | ||
| <!-- Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref` | ||
| keyword _creates_ a reference, for use in the pattern. If you need a mutable | ||
| reference, `ref mut` will work in the same way: | ||
| reference, `ref mut` will work in the same way: --> | ||
| ここで、 `match` 内の `r` は `&i32` 型を持っています。言い換えると、 `ref` キーワードがリファレンスを _作ります_ 。 | ||
|
|
||
| ```rust | ||
| let mut x = 5; | ||
|
|
@@ -217,9 +250,11 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| # Ranges | ||
| <!-- # Ranges --> | ||
| # レンジ | ||
|
|
||
| You can match a range of values with `...`: | ||
| <!-- You can match a range of values with `...`: --> | ||
| `...` で値のレンジのマッチを行うことが出来ます。 | ||
|
|
||
| ```rust | ||
| let x = 1; | ||
|
|
@@ -230,9 +265,10 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `one through five`. | ||
|
|
||
| Ranges are mostly used with integers and `char`s: | ||
| <!-- This prints `one through five`. --> | ||
| これは `one through five` を出力します。 | ||
| <!-- Ranges are mostly used with integers and `char`s: --> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 空行が1行抜けています。 |
||
| レンジは大体、整数か `char` 型で使われます。 | ||
|
|
||
| ```rust | ||
| let x = '💅'; | ||
|
|
@@ -244,11 +280,14 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `something else`. | ||
| <!-- This prints `something else`. --> | ||
| これは `something else` を出力します。 | ||
|
|
||
| # Bindings | ||
| <!-- # Bindings --> | ||
| # 束縛 | ||
|
|
||
| You can bind values to names with `@`: | ||
| <!-- You can bind values to names with `@`: --> | ||
| `@` で値を名前と結びつけることが出来ます。 | ||
|
|
||
| ```rust | ||
| let x = 1; | ||
|
|
@@ -259,8 +298,10 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `got a range element 1`. This is useful when you want to | ||
| do a complicated match of part of a data structure: | ||
| <!-- This prints `got a range element 1`. This is useful when you want to | ||
| do a complicated match of part of a data structure: --> | ||
| これは `got a range element 1` を出力します。 | ||
| データ構造の一部に対する複雑なマッチが欲しいときに有用です。 | ||
|
|
||
| ```rust | ||
| #[derive(Debug)] | ||
|
|
@@ -276,10 +317,12 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `Some("Steve")`: we’ve bound the inner `name` to `a`. | ||
| <!--This prints `Some("Steve")`: we’ve bound the inner `name` to `a`.--> | ||
| これは `Some("Steve")` を出力します。内側の `name` を `a` に結びつけます。 | ||
| <!-- If you use `@` with `|`, you need to make sure the name is bound in each part | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 空行が一行消えています。 |
||
| of the pattern: --> | ||
| もし `|` で `@` を使うときは、パターンのそれぞれの部分が名前と結びついているか確認する必要があります。 | ||
|
|
||
| If you use `@` with `|`, you need to make sure the name is bound in each part | ||
| of the pattern: | ||
|
|
||
| ```rust | ||
| let x = 5; | ||
|
|
@@ -290,9 +333,11 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| # Guards | ||
| <!-- # Guards --> | ||
| # ガード | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [対訳表] |
||
|
|
||
| You can introduce ‘match guards’ with `if`: | ||
| <!--You can introduce ‘match guards’ with `if`: --> | ||
| `if` を使うことでマッチガードを導入することが出来ます。 | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 翻訳と原文の間に1行空行が出来てしまっているようです。 |
||
| ```rust | ||
| enum OptionalInt { | ||
|
|
@@ -309,9 +354,11 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `Got an int!`. | ||
| <!--This prints `Got an int!`. --> | ||
| これは `Got an int!` を出力します。 | ||
|
|
||
| If you’re using `if` with multiple patterns, the `if` applies to both sides: | ||
| <!--If you’re using `if` with multiple patterns, the `if` applies to both sides:--> | ||
| 複式パターンで `if` を使うと、 `if` は両方に適用されます。 | ||
|
|
||
| ```rust | ||
| let x = 4; | ||
|
|
@@ -323,28 +370,33 @@ match x { | |
| } | ||
| ``` | ||
|
|
||
| This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to | ||
| just the `5`. In other words, the precedence of `if` behaves like this: | ||
| <!--This prints `no`, because the `if` applies to the whole of `4 | 5`, and not to | ||
| just the `5`. In other words, the precedence of `if` behaves like this: --> | ||
| これは `no` を出力します。なぜなら `if` は `4 | 5` 全体に適用されるのであって、 `5` 単独に対してではないからです。つまり、 `if` 節は以下のように振舞います。 | ||
|
|
||
| ```text | ||
| (4 | 5) if y => ... | ||
| ``` | ||
|
|
||
| not this: | ||
| <!--not this: --> | ||
| 次のようには解釈されません。 | ||
|
|
||
| ```text | ||
| 4 | (5 if y) => ... | ||
| ``` | ||
|
|
||
| # Mix and Match | ||
| <!-- # Mix and Match --> | ||
| # 混ぜてマッチ | ||
|
|
||
| Whew! That’s a lot of different ways to match things, and they can all be | ||
| mixed and matched, depending on what you’re doing: | ||
| <!--Whew! That’s a lot of different ways to match things, and they can all be | ||
| mixed and matched, depending on what you’re doing: --> | ||
| さて、マッチにはまだ沢山の方法があります。やろうとしていることに依りますが、それらの方法を混ぜてマッチさせることも出来ます。 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
条件に依って出来たり出来なかったりするというよりは自分の使い方に合わせて色々交ぜれるという話なので |
||
|
|
||
| ```rust,ignore | ||
| match x { | ||
| Foo { x: Some(ref name), y: None } => ... | ||
| } | ||
| ``` | ||
|
|
||
| Patterns are very powerful. Make good use of them. | ||
| <!-- Patterns are very powerful. Make good use of them. --> | ||
| パターンはとても強力です。上手に使いましょう。 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
パターンは方法というよりは構文上の構成要素ですし、「極めて一般的です」「とてもありふれています」あたりの表現でいいと思います。