Skip to content

If let #45

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

Merged
merged 2 commits into from
Feb 1, 2016
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
46 changes: 30 additions & 16 deletions 1.6/ja/book/if-let.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
% if let
<!-- % if let -->

`if let` allows you to combine `if` and `let` together to reduce the overhead
of certain kinds of pattern matches.
<!-- `if let` allows you to combine `if` and `let` together to reduce the overhead -->
<!-- of certain kinds of pattern matches. -->
`if let` によって`if` と `let` を一体化して用いることが可能となり、
ある種のパターンマッチに伴うオーバーヘッドを削減することができます。

For example, let’s say we have some sort of `Option<T>`. We want to call a function
on it if it’s `Some<T>`, but do nothing if it’s `None`. That looks like this:
<!-- For example, let’s say we have some sort of `Option<T>`. We want to call a function -->
<!-- on it if it’s `Some<T>`, but do nothing if it’s `None`. That looks like this: -->
例えば今、 `Option<T>` 型の値が有るとして、
この値が `Some<T>` であるならば何らかの関数を呼び出し、`None` ならば何もしたくないとしましょう
そのような処理は例えば以下のように書けるでしょう:

```rust
# let option = Some(5);
Expand All @@ -15,7 +21,8 @@ match option {
}
```

We don’t have to use `match` here, for example, we could use `if`:
<!-- We don’t have to use `match` here, for example, we could use `if`: -->
このような場合 `match` を用いなくても良く、 `if` を使って以下のように書けます:

```rust
# let option = Some(5);
Expand All @@ -26,8 +33,9 @@ if option.is_some() {
}
```

Neither of these options is particularly appealing. We can use `if let` to
do the same thing in a nicer way:
<!-- Neither of these options is particularly appealing. We can use `if let` to -->
<!-- do the same thing in a nicer way: -->
上述のコードのどちらもまだ理想的ではありません。 `if let` を用いてより良い方法で記述できます:

```rust
# let option = Some(5);
Expand All @@ -37,12 +45,15 @@ if let Some(x) = option {
}
```

If a [pattern][patterns] matches successfully, it binds any appropriate parts of
the value to the identifiers in the pattern, then evaluates the expression. If
the pattern doesn’t match, nothing happens.
<!-- If a [pattern][patterns] matches successfully, it binds any appropriate parts of -->
<!-- the value to the identifiers in the pattern, then evaluates the expression. If -->
<!-- the pattern doesn’t match, nothing happens. -->
もし [パターン][patterns] マッチが成功した場合、パターンに含まれる変数に適切に値が割り当てられ、
式が評価されます。もしパターンマッチが失敗した場合には何も起こりません。

If you want to do something else when the pattern does not match, you can
use `else`:
<!-- If you want to do something else when the pattern does not match, you can -->
<!-- use `else`: -->
もしパターンマッチに失敗した場合に実行したいコードが有る場合は `else` を使うことができます:

```rust
# let option = Some(5);
Expand All @@ -57,8 +68,10 @@ if let Some(x) = option {

## `while let`

In a similar fashion, `while let` can be used when you want to conditionally
loop as long as a value matches a certain pattern. It turns code like this:
<!-- In a similar fashion, `while let` can be used when you want to conditionally -->
<!-- loop as long as a value matches a certain pattern. It turns code like this: -->
同じように、 `while let` を使うことで、値がパターンにマッチし続ける限り繰り返し実行することができます。
例えば以下の様なコードが有るときに:

```rust
let mut v = vec![1, 3, 5, 7, 11];
Expand All @@ -70,7 +83,8 @@ loop {
}
```

Into code like this:
<!-- Into code like this: -->
`when let` を用いることで、以下のように書くことができます:

```rust
let mut v = vec![1, 3, 5, 7, 11];
Expand All @@ -79,4 +93,4 @@ while let Some(x) = v.pop() {
}
```

[patterns]: patterns.html
[パターン]: patterns.html