diff --git a/1.6/ja/book/patterns.md b/1.6/ja/book/patterns.md index 8f4a7a43..b587af1a 100644 --- a/1.6/ja/book/patterns.md +++ b/1.6/ja/book/patterns.md @@ -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 + + +パターンはRustにおいて極めて一般的な方法です。 + +パターンは、[変数束縛][bindings]、[マッチ文][match]、などで使われています。 + +さあ、めくるめくパターンの旅を始めましょう! [bindings]: variable-bindings.html [match]: match.html -A quick refresher: you can match against literals directly, and `_` acts as an -‘any’ case: + +簡単な復習: パターンはリテラルに対しては直接マッチさせることができます。また、 `_` は「any」型として振る舞います。 ```rust let x = 1; @@ -21,10 +27,11 @@ match x { } ``` -This prints `one`. + +これは `one` を表示します。 -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: + +これの結果は以下のようになります。 ```text x: c c: c x: x ``` -In other words, `x =>` matches the pattern and introduces a new binding named + +説明すると、 `x =>` はパターンへのマッチだけでなく、マッチの腕内で有効な `x` という名前の束縛を導入します。なぜなら既に `x` は束縛されており、この新しい `x` はそれを覆い隠します。 -# Multiple patterns + +# 複式パターン + + + `|` を使うと、複式パターンが導入出来ます。 -You can match multiple patterns with `|`: ```rust let x = 1; @@ -62,12 +74,15 @@ match x { } ``` -This prints `one or two`. + +これは、 `one or two` を出力します。 -# Destructuring + +# デストラクチャ -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. + +値に別の名前を付けたいときは、 `:` を使うことが出来ます。 ```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: + +値のうちいくつかを扱いたい場合は、値の全てに名前を付ける必要はありません。 ```rust struct Point { @@ -114,9 +131,11 @@ match origin { } ``` -This prints `x is 0`. + +これは `x is 0` を出力します。 -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`. + +これは `y is 0` を出力します。 + + +この「デストラクチャリング」と呼ばれる振る舞いは、[タプル][tuples]や[列挙型][enum]のような、構成されたデータ型で起こります。 -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 + +# 束縛の無視 -You can use `_` in a pattern to disregard the type and value. -For example, here’s a `match` against a `Result`: + +パターン内の型や値を無視するために `_` を使うことが出来ます。 + + +例として、 `Result` に対して `match` を適用してみましょう。 ```rust # let some_value: Result = 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 + +最初の部分では、 `Ok` ヴァリアント内の値を `value` に結びつけています。しかし、 `Err` 部分ですと、特定のエラーを避けるために、また標準エラーメッセージを表示するために `_` を使っています。 + + `_` は束縛を伴うどんなパターンに於いても有効です。これは大きな構造の一部分を無視する際に有用です。 ```rust fn coordinate() -> (i32, i32, i32) { @@ -168,10 +195,11 @@ fn coordinate() -> (i32, i32, i32) { let (x, _, z) = coordinate(); ``` -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. + +ここでは、タプルの最初と最後の要素を `x` と `z` に結びつけています。 + +同様に、 `..` でパターン内の複数の値を無視することが出来ます。 ```rust enum OptionalTuple { @@ -187,11 +215,14 @@ match x { } ``` -This prints `Got a tuple!`. + +これは `Got a tuple!` を出力します。 -# ref and ref mut + +# ref と ref mut -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`. + +これは `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` + +ここで、 `match` 内の `r` は `&i32` 型を持っています。言い換えると、 `ref` キーワードがリファレンスを _作ります_ 。 ```rust let mut x = 5; @@ -217,9 +250,11 @@ match x { } ``` -# Ranges + +# レンジ -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: + +これは `one through five` を出力します。 + +レンジは大体、整数か `char` 型で使われます。 ```rust let x = '💅'; @@ -244,11 +280,14 @@ match x { } ``` -This prints `something else`. + +これは `something else` を出力します。 -# Bindings + +# 束縛 -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: + +これは `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`. + +これは `Some("Steve")` を出力します。内側の `name` を `a` に結びつけます。 + +もし `|` で `@` を使うときは、パターンのそれぞれの部分が名前と結びついているか確認する必要があります。 -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 + +# ガード -You can introduce ‘match guards’ with `if`: + + `if` を使うことでマッチガードを導入することが出来ます。 ```rust enum OptionalInt { @@ -309,9 +354,11 @@ match x { } ``` -This prints `Got an int!`. + +これは `Got an int!` を出力します。 -If you’re using `if` with multiple patterns, the `if` applies to both sides: + +複式パターンで `if` を使うと、 `if` は両方に適用されます。 ```rust let x = 4; @@ -323,23 +370,27 @@ 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: + +これは `no` を出力します。なぜなら `if` は `4 | 5` 全体に適用されるのであって、 `5` 単独に対してではないからです。つまり、 `if` 節は以下のように振舞います。 ```text (4 | 5) if y => ... ``` -not this: + +次のようには解釈されません。 ```text 4 | (5 if y) => ... ``` -# 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: + +さて、マッチにはまだ沢山の方法があります。やろうとしていることに依りますが、それらの方法を混ぜてマッチさせることも出来ます。 ```rust,ignore match x { @@ -347,4 +398,5 @@ match x { } ``` -Patterns are very powerful. Make good use of them. + +パターンはとても強力です。上手に使いましょう。 diff --git a/TranslationTable.md b/TranslationTable.md index 4a490ef2..950ae726 100644 --- a/TranslationTable.md +++ b/TranslationTable.md @@ -61,6 +61,7 @@ | generic parameter | ジェネリックパラメータ | generics | ジェネリクス | growable | 伸張可能 +| guard | ガード | hash | ハッシュ | identifier | 識別子 | immutable | イミュータブル @@ -107,6 +108,7 @@ | platform | プラットフォーム | pointer | ポインタ | process | プロセス +| range | レンジ | raw pointer | 生ポインタ | re-assignment | 再代入 | rebind | 再束縛