Skip to content

Commit fb41721

Browse files
committed
Merge pull request #70 from yohhoy/if
4.5 if
2 parents 795624b + 561a4b1 commit fb41721

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

1.6/ja/book/if.md

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,73 @@
11
% if
2+
<!-- % if -->
23

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+
さっそく説明していきましょう。
610

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+
取りうる複数のパスから、選択の決定を行うポイントを表します。
1016

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つを選択します。
1219

1320
```rust
1421
let x = 5;
1522

1623
if x == 5 {
17-
println!("x is five!");
24+
# // println!("x is five!");
25+
println!("x は 5 です!");
1826
}
1927
```
2028

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` の場合、ブロックは実行されません。
2435

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` を使います:
2638

2739
```rust
2840
let x = 5;
2941

3042
if x == 5 {
31-
println!("x is five!");
43+
# // println!("x is five!");
44+
println!("x は 5 です!");
3245
} else {
33-
println!("x is not five :(");
46+
# // println!("x is not five :(");
47+
println!("x は 5 ではありません :(");
3448
}
3549
```
3650

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` を使います:
3853

3954
```rust
4055
let x = 5;
4156

4257
if x == 5 {
43-
println!("x is five!");
58+
# // println!("x is five!");
59+
println!("x は 5 です!");
4460
} else if x == 6 {
45-
println!("x is six!");
61+
# // println!("x is six!");
62+
println!("x は 6 です!");
4663
} else {
47-
println!("x is not five or six :(");
64+
# // println!("x is not five or six :(");
65+
println!("x は 5 でも 6 でもありません :(");
4866
}
4967
```
5068

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+
全くもって普通ですね。しかし、次のような使い方もできるのです:
5271

5372
```rust
5473
let x = 5;
@@ -60,14 +79,18 @@ let y = if x == 5 {
6079
}; // y: i32
6180
```
6281

63-
Which we can (and probably should) write like this:
82+
<!-- Which we can (and probably should) write like this: -->
83+
次のように書くこともできます(そして、大抵はこう書くべきです):
6484

6585
```rust
6686
let x = 5;
6787

6888
let y = if x == 5 { 10 } else { 15 }; // y: i32
6989
```
7090

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

Comments
 (0)