Skip to content

Commit 945286f

Browse files
committed
Merge pull request #45 from pocket7878/if-let
If let
2 parents 5f88859 + aa8025b commit 945286f

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

1.6/ja/book/if-let.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
% if let
2+
<!-- % if let -->
23

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

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

915
```rust
1016
# let option = Some(5);
@@ -15,7 +21,8 @@ match option {
1521
}
1622
```
1723

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

2027
```rust
2128
# let option = Some(5);
@@ -26,8 +33,9 @@ if option.is_some() {
2633
}
2734
```
2835

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

3240
```rust
3341
# let option = Some(5);
@@ -37,12 +45,15 @@ if let Some(x) = option {
3745
}
3846
```
3947

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

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

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

5869
## `while let`
5970

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

6376
```rust
6477
let mut v = vec![1, 3, 5, 7, 11];
@@ -70,7 +83,8 @@ loop {
7083
}
7184
```
7285

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

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

82-
[patterns]: patterns.html
96+
[パターン]: patterns.html

0 commit comments

Comments
 (0)