@@ -8,51 +8,66 @@ Rustにおける `if` の扱いはさほど複雑ではありませんが、伝
8
8
動的型付け言語でみられる ` if ` にずっと近いものになっています。そのニュアンスをしっかり理解できるよう、
9
9
さっそく説明していきましょう。
10
10
11
- ` if ` is a specific form of a more general concept, the ‘branch’. The name comes
12
- from a branch in a tree: a decision point, where depending on a choice,
13
- multiple paths can be taken.
11
+ <!-- `if` is a specific form of a more general concept, the ‘branch’. The name comes -->
12
+ <!-- from a branch in a tree: a decision point, where depending on a choice, -->
13
+ <!-- multiple paths can be taken. -->
14
+ ` if ` は一般化されたコンセプト、「分岐(branch)」の特別な形式です。この名前は木の枝(branch)を由来とし:
15
+ 取りうる複数のパスから、選択の決定を行うポイントを表します。
14
16
15
- In the case of ` if ` , there is one choice that leads down two paths:
17
+ <!-- In the case of `if`, there is one choice that leads down two paths: -->
18
+ ` if ` の場合は、続く2つのパスから1つを選択します。
16
19
17
20
``` rust
18
21
let x = 5 ;
19
22
20
23
if x == 5 {
21
- println! (" x is five!" );
24
+ # // println!("x is five!");
25
+ println! (" x は 5 です!" );
22
26
}
23
27
```
24
28
25
- If we changed the value of ` x ` to something else, this line would not print.
26
- More specifically, if the expression after the ` if ` evaluates to ` true ` , then
27
- the block is executed. If it’s ` false ` , then it is not.
29
+ <!-- If we changed the value of `x` to something else, this line would not print. -->
30
+ <!-- More specifically, if the expression after the `if` evaluates to `true`, then -->
31
+ <!-- the block is executed. If it’s `false`, then it is not. -->
32
+ 仮に ` x ` を別の値へと変更すると、この行は表示されません。より正確に言うなら、
33
+ ` if ` のあとにくる式が ` true ` に評価された場合に、ブロックが実行されます。
34
+ ` false ` の場合、ブロックは実行されません。
28
35
29
- If you want something to happen in the ` false ` case, use an ` else ` :
36
+ <!-- If you want something to happen in the `false` case, use an `else`: -->
37
+ ` false ` の場合にも何かをしたいなら、 ` else ` を使います:
30
38
31
39
``` rust
32
40
let x = 5 ;
33
41
34
42
if x == 5 {
35
- println! (" x is five!" );
43
+ # // println!("x is five!");
44
+ println! (" x は 5 です!" );
36
45
} else {
37
- println! (" x is not five :(" );
46
+ # // println!("x is not five :(");
47
+ println! (" x は 5 ではありません :(" );
38
48
}
39
49
```
40
50
41
- If there is more than one case, use an ` else if ` :
51
+ <!-- If there is more than one case, use an `else if`: -->
52
+ 場合分けが複数あるときは、 ` else if ` を使います:
42
53
43
54
``` rust
44
55
let x = 5 ;
45
56
46
57
if x == 5 {
47
- println! (" x is five!" );
58
+ # // println!("x is five!");
59
+ println! (" x は 5 です!" );
48
60
} else if x == 6 {
49
- println! (" x is six!" );
61
+ # // println!("x is six!");
62
+ println! (" x は 6 です!" );
50
63
} else {
51
- println! (" x is not five or six :(" );
64
+ # // println!("x is not five or six :(");
65
+ println! (" x は 5 でも 6 でもありません :(" );
52
66
}
53
67
```
54
68
55
- This is all pretty standard. However, you can also do this:
69
+ <!-- This is all pretty standard. However, you can also do this: -->
70
+ 全くもって普通ですね。しかし、次のような使い方もできるのです:
56
71
57
72
``` rust
58
73
let x = 5 ;
@@ -64,14 +79,18 @@ let y = if x == 5 {
64
79
}; // y: i32
65
80
```
66
81
67
- Which we can (and probably should) write like this:
82
+ <!-- Which we can (and probably should) write like this: -->
83
+ 次のように書くこともできます(そして、大抵はこう書くべきです):
68
84
69
85
``` rust
70
86
let x = 5 ;
71
87
72
88
let y = if x == 5 { 10 } else { 15 }; // y: i32
73
89
```
74
90
75
- This works because ` if ` is an expression. The value of the expression is the
76
- value of the last expression in whichever branch was chosen. An ` if ` without an
77
- ` else ` always results in ` () ` as the value.
91
+ <!-- This works because `if` is an expression. The value of the expression is the -->
92
+ <!-- value of the last expression in whichever branch was chosen. An `if` without an -->
93
+ <!-- `else` always results in `()` as the value. -->
94
+ これが出来るのは ` if ` が式であるためです。その式の値は、選択された分岐中の最後の式の値となります。
95
+ ` else ` のない ` if ` では、その値は常に ` () ` となります。
96
+
0 commit comments