@@ -38,8 +38,11 @@ fn main() {
38
38
39
39
// Error! There are limitations in conversion rules.
40
40
// A float cannot be directly converted to a char.
41
+ // エラー! 変換ルールには制限があります。
42
+ // 浮動小数点数を文字に直接変換することはできません。
41
43
let character = decimal as char;
42
44
// FIXME ^ Comment out this line
45
+ // FIXME ^ この行をコメントアウトしましょう。
43
46
44
47
println!("Casting: {} -> {} -> {}", decimal, integer, character);
45
48
@@ -62,6 +65,7 @@ fn main() {
62
65
println!(" -1 as a u8 is : {}", (-1i8) as u8);
63
66
64
67
// For positive numbers, this is the same as the modulus
68
+ // 正の数では、これは剰余と同じです。
65
69
println!("1000 mod 256 is : {}", 1000 % 256);
66
70
67
71
// When casting to a signed type, the (bitwise) result is the same as
@@ -72,6 +76,7 @@ fn main() {
72
76
// 2. 2の補数(two's complement)をとる
73
77
74
78
// Unless it already fits, of course.
79
+ // すでに収まっている場合はそのままです。
75
80
println!(" 128 as a i16 is: {}", 128 as i16);
76
81
77
82
// 128 as u8 -> 128, whose value in 8-bit two's complement representation is:
@@ -90,6 +95,10 @@ fn main() {
90
95
// when casting from float to int. If the floating point value exceeds
91
96
// the upper bound or is less than the lower bound, the returned value
92
97
// will be equal to the bound crossed.
98
+ // Rust 1.45以降、浮動小数点数を整数にキャストするとき、
99
+ // `as`キーワードが *飽和的キャスト* を行います。
100
+ // 浮動小数点数の値が上限を超えたり下限を下回ったりする場合は、
101
+ // 戻り値は越えられた境界の値となります。
93
102
94
103
// 300.0 as u8 is 255
95
104
println!(" 300.0 as u8 is : {}", 300.0_f32 as u8);
@@ -101,6 +110,9 @@ fn main() {
101
110
// This behavior incurs a small runtime cost and can be avoided
102
111
// with unsafe methods, however the results might overflow and
103
112
// return **unsound values**. Use these methods wisely:
113
+ // この挙動は実行時にややコストがかかるため、安全でない方法で回避できます。
114
+ // ただし、結果はオーバーフローしたり *不正確な値* を返す場合があります。
115
+ // この方法は賢く使いましょう:
104
116
unsafe {
105
117
// 300.0 as u8 is 44
106
118
println!(" 300.0 as u8 is : {}", 300.0_f32.to_int_unchecked::<u8>());
0 commit comments