1
1
% if
2
+ <!-- % if -->
2
3
3
- Rust’s take on ` if ` is not particularly complex, but it’s much more like the
4
- ` if ` you’ll find in a dynamically typed language than in a more traditional
5
- systems language. So let’s talk about it, to make sure you grasp the nuances.
4
+ <!-- Rust’s take on `if` is not particularly complex, but it’s much more like the -->
5
+ <!-- `if` you’ll find in a dynamically typed language than in a more traditional -->
6
+ <!-- systems language. So let’s talk about it, to make sure you grasp the nuances. -->
7
+ Rustにおける ` if ` の扱いはさほど複雑ではありませんが、伝統的なシステムプログラミング言語のそれに比べて、
8
+ 動的型付け言語でみられる ` if ` にずっと近いものになっています。そのニュアンスをしっかり理解できるよう、
9
+ さっそく説明していきましょう。
6
10
7
- ` if ` is a specific form of a more general concept, the ‘branch’. The name comes
8
- from a branch in a tree: a decision point, where depending on a choice,
9
- 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
+ 取りうる複数のパスから、選択の決定を行うポイントを表します。
10
16
11
- 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つを選択します。
12
19
13
20
``` rust
14
21
let x = 5 ;
15
22
16
23
if x == 5 {
17
- println! (" x is five!" );
24
+ # // println!("x is five!");
25
+ println! (" x は 5 です!" );
18
26
}
19
27
```
20
28
21
- If we changed the value of ` x ` to something else, this line would not print.
22
- More specifically, if the expression after the ` if ` evaluates to ` true ` , then
23
- 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 ` の場合、ブロックは実行されません。
24
35
25
- 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 ` を使います:
26
38
27
39
``` rust
28
40
let x = 5 ;
29
41
30
42
if x == 5 {
31
- println! (" x is five!" );
43
+ # // println!("x is five!");
44
+ println! (" x は 5 です!" );
32
45
} else {
33
- println! (" x is not five :(" );
46
+ # // println!("x is not five :(");
47
+ println! (" x は 5 ではありません :(" );
34
48
}
35
49
```
36
50
37
- 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 ` を使います:
38
53
39
54
``` rust
40
55
let x = 5 ;
41
56
42
57
if x == 5 {
43
- println! (" x is five!" );
58
+ # // println!("x is five!");
59
+ println! (" x は 5 です!" );
44
60
} else if x == 6 {
45
- println! (" x is six!" );
61
+ # // println!("x is six!");
62
+ println! (" x は 6 です!" );
46
63
} else {
47
- println! (" x is not five or six :(" );
64
+ # // println!("x is not five or six :(");
65
+ println! (" x は 5 でも 6 でもありません :(" );
48
66
}
49
67
```
50
68
51
- This is all pretty standard. However, you can also do this:
69
+ <!-- This is all pretty standard. However, you can also do this: -->
70
+ 全くもって普通ですね。しかし、次のような使い方もできるのです:
52
71
53
72
``` rust
54
73
let x = 5 ;
@@ -60,14 +79,18 @@ let y = if x == 5 {
60
79
}; // y: i32
61
80
```
62
81
63
- Which we can (and probably should) write like this:
82
+ <!-- Which we can (and probably should) write like this: -->
83
+ 次のように書くこともできます(そして、大抵はこう書くべきです):
64
84
65
85
``` rust
66
86
let x = 5 ;
67
87
68
88
let y = if x == 5 { 10 } else { 15 }; // y: i32
69
89
```
70
90
71
- This works because ` if ` is an expression. The value of the expression is the
72
- value of the last expression in whichever branch was chosen. An ` if ` without an
73
- ` 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