1
1
% if let
2
+ <!-- % if let -->
2
3
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
+ ある種のパターンマッチに伴うオーバーヘッドを削減することができます。
5
8
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
+ そのような処理は例えば以下のように書けるでしょう:
8
14
9
15
``` rust
10
16
# let option = Some (5 );
@@ -15,7 +21,8 @@ match option {
15
21
}
16
22
```
17
23
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 ` を使って以下のように書けます:
19
26
20
27
``` rust
21
28
# let option = Some (5 );
@@ -26,8 +33,9 @@ if option.is_some() {
26
33
}
27
34
```
28
35
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 ` を用いてより良い方法で記述できます:
31
39
32
40
``` rust
33
41
# let option = Some (5 );
@@ -37,12 +45,15 @@ if let Some(x) = option {
37
45
}
38
46
```
39
47
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
+ 式が評価されます。もしパターンマッチが失敗した場合には何も起こりません。
43
53
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 ` を使うことができます:
46
57
47
58
``` rust
48
59
# let option = Some (5 );
@@ -57,8 +68,10 @@ if let Some(x) = option {
57
68
58
69
## ` while let `
59
70
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
+ 例えば以下の様なコードが有るときに:
62
75
63
76
``` rust
64
77
let mut v = vec! [1 , 3 , 5 , 7 , 11 ];
70
83
}
71
84
```
72
85
73
- Into code like this:
86
+ <!-- Into code like this: -->
87
+ ` when let ` を用いることで、以下のように書くことができます:
74
88
75
89
``` rust
76
90
let mut v = vec! [1 , 3 , 5 , 7 , 11 ];
@@ -79,4 +93,4 @@ while let Some(x) = v.pop() {
79
93
}
80
94
```
81
95
82
- [ patterns ] : patterns.html
96
+ [ パターン ] : patterns.html
0 commit comments