11% if let
22<!-- % if let -->
33
4- ` if let ` allows you to combine ` if ` and ` let ` together to reduce the overhead
5- 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+ ある種のパターンマッチに伴うオーバーヘッドを削減することができます。
68
7- For example, let’s say we have some sort of ` Option<T> ` . We want to call a function
8- 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+ そのような処理は例えば以下のように書けるでしょう:
914
1015``` rust
1116# let option = Some (5 );
@@ -16,7 +21,8 @@ match option {
1621}
1722```
1823
19- 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 ` を使って以下のように書けます:
2026
2127``` rust
2228# let option = Some (5 );
@@ -27,8 +33,9 @@ if option.is_some() {
2733}
2834```
2935
30- Neither of these options is particularly appealing. We can use ` if let ` to
31- 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 ` を用いてより良い方法で記述できます:
3239
3340``` rust
3441# let option = Some (5 );
@@ -38,12 +45,15 @@ if let Some(x) = option {
3845}
3946```
4047
41- If a [ pattern] [ patterns ] matches successfully, it binds any appropriate parts of
42- the value to the identifiers in the pattern, then evaluates the expression. If
43- 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+ 式が評価されます。もしパターンマッチが失敗した場合には何も起こりません。
4453
45- If you want to do something else when the pattern does not match, you can
46- use ` else ` :
54+ <!-- If you want to do something else when the pattern does not match, you can -->
55+ <!-- use `else`: -->
56+ もしパターンマッチに失敗した場合に実行したいコードが有る場合は ` else ` を使うことができます:
4757
4858``` rust
4959# let option = Some (5 );
@@ -58,8 +68,10 @@ if let Some(x) = option {
5868
5969## ` while let `
6070
61- In a similar fashion, ` while let ` can be used when you want to conditionally
62- 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+ 例えば以下の様なコードが有るときに:
6375
6476``` rust
6577let mut v = vec! [1 , 3 , 5 , 7 , 11 ];
7183}
7284```
7385
74- Into code like this:
86+ <!-- Into code like this: -->
87+ ` when let ` を用いることで、以下のように書くことができます:
7588
7689``` rust
7790let mut v = vec! [1 , 3 , 5 , 7 , 11 ];
@@ -80,4 +93,4 @@ while let Some(x) = v.pop() {
8093}
8194```
8295
83- [ patterns ] : patterns.html
96+ [ パターン ] : patterns.html
0 commit comments