Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 deletions 1.6/ja/book/patterns.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
% パターン Patterns
<!--
% Patterns
-->
<!--Patterns are quite common in Rust. -->
パターンはRustにおいて極めて一般的な方法です。
% パターン
<!-- % Patterns -->

<!-- Patterns are quite common in Rust. -->
パターンはRustにおいて極めて一般的です。

<!-- We use them in [variable
bindings][bindings], [match statements][match], and other places, too.-->
パターンは、[変数束縛][bindings]、[マッチ文][match]、などで使われています。
パターンは [変数束縛][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: -->
簡単な復習: パターンはリテラルに対しては直接マッチさせることができます。また、 `_` は「any」型として振る舞います
簡単な復習:リテラルに対しては直接マッチさせられます。また、 `_` は「任意の」ケースとして振る舞います

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

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

```rust
let x = 'x';
Expand All @@ -45,7 +47,7 @@ println!("x: {}", x)
```

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

```text
x: c c: c
Expand All @@ -55,13 +57,13 @@ x: x
<!-- 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 =>` はパターンへのマッチだけでなく、マッチの腕内で有効な `x` という名前の束縛を導入します。なぜなら既に `x` は束縛されており、この新しい `x` はそれを覆い隠します。
別の言い方をすると、 `x =>` はパターンへのマッチだけでなく、マッチの腕内で有効な `x` という名前の束縛を導入します。既に `x` は束縛されていたので、この新しい `x` はそれを覆い隠します。

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

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


```rust
Expand All @@ -78,11 +80,11 @@ match x {
これは、 `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]のようなデータ型を作成したいとき、パターン内でデータを分配することが出来ます
例えば [`struct`][struct] のような複合データ型を作成したいとき、パターン内でデータを分解することができます

```rust
struct Point {
Expand All @@ -100,7 +102,7 @@ match origin {
[struct]: structs.html

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

```rust
struct Point {
Expand All @@ -116,7 +118,7 @@ match origin {
```

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

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

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

```rust
struct Point {
Expand All @@ -155,8 +157,7 @@ match origin {

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

この「デストラクチャリング(destructuring)」と呼ばれる振る舞いは、 [タプル][tuples] や [列挙型][enums] のような、複合データ型で使用できます。

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

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

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

```rust
# let some_value: Result<i32, &'static str> = Err("There was an error");
Expand All @@ -181,14 +182,16 @@ match some_value {
<!-- 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. -->
最初の部分では、 `Ok` ヴァリアント内の値を `value` に結びつけています。しかし、 `Err` 部分ですと、特定のエラーを避けるために、また標準エラーメッセージを表示するために `_` を使っています。
最初の部分では `Ok` ヴァリアント内の値を `value` に結びつけています。しかし `Err` 部分では、特定のエラーを避けて、標準的なエラーメッセージを表示するために `_` を使っています。

<!-- `_` is valid in any pattern that creates a binding. This can be useful to
ignore parts of a larger structure: -->
`_` は束縛を伴うどんなパターンに於いても有効です。これは大きな構造の一部分を無視する際に有用です。
`_` は束縛を伴うどんなパターンにおいても有効です。これは大きな構造の一部分を無視する際に有用です。

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

Expand All @@ -198,8 +201,9 @@ let (x, _, z) = coordinate();
<!-- 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. -->
同様に、 `..` でパターン内の複数の値を無視することが出来ます。

<!-- Similarly, you can use `..` in a pattern to disregard multiple values. -->
同様に `..` でパターン内の複数の値を無視することができます。

```rust
enum OptionalTuple {
Expand All @@ -222,7 +226,7 @@ match x {
# ref と ref mut

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

```rust
let x = 5;
Expand All @@ -240,7 +244,7 @@ match x {
<!-- 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: -->
ここで `match` 内の `r` は `&i32` 型を持っています。言い換えると `ref` キーワードがリファレンスを _作ります_ 。
ここで `match` 内の `r` は `&i32` 型を持っています。言い換えると `ref` キーワードがリファレンスを _作ります_ 。

```rust
let mut x = 5;
Expand All @@ -254,7 +258,7 @@ match x {
# レンジ

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

```rust
let x = 1;
Expand All @@ -267,8 +271,9 @@ match x {

<!-- This prints `one through five`. -->
これは `one through five` を出力します。

<!-- Ranges are mostly used with integers and `char`s: -->
レンジは大体、整数か `char` 型で使われます
レンジは大体、整数か `char` 型で使われます

```rust
let x = '💅';
Expand All @@ -287,7 +292,7 @@ match x {
# 束縛

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

```rust
let x = 1;
Expand All @@ -301,7 +306,7 @@ 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)]
Expand All @@ -319,10 +324,10 @@ 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;
Expand All @@ -337,7 +342,7 @@ match x {
# ガード

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

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

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

```rust
let x = 4;
Expand All @@ -372,14 +377,14 @@ 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` 節は以下のように振舞います
これは `no` を出力します。なぜなら `if` は `4 | 5` 全体に適用されるのであって、 `5` 単独に対してではないからです。つまり `if` 節は以下のように振舞います

```text
(4 | 5) if y => ...
```

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

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

<!--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 {
Expand Down