Repository navigation
Expand file tree
/
Copy pathtypes.rs
More file actions
49 lines (40 loc) · 1.54 KB
/
Copy pathtypes.rs
File metadata and controls
49 lines (40 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
///类型
pub fn types() {
let decimal = 65.4321_f32;
//不支持隐式转化
//let integer: u8 = decimal;
// FIXME ^ Comment out this line
//显示转化
let integer = decimal as u8;
let character = integer as char;
println!("Casting: {} -> {} -> {}", decimal, integer, character);
// 1000 already fits in a u16
println!("1000 as a u16 is: {}", 1000 as u16);
// 1000 - 256 - 256 - 256 = 232
// Under the hood, the first 8 least significant bits (LSB) are kept,
// while the rest towards the most significant bit (MSB) get truncated.
//println!("1000 as a u8 is : {}", 1000 as u8);
// -1 + 256 = 255
println!(" -1 as a u8 is : {}", (-1i8) as u8);
println!("1000 mod 256 is : {}", 1000 % 256);
println!(" 128 as a i16 is: {}", 128 as i16);
//println!(" 128 as a i8 is : {}", 128 as i8);
//println!("1000 as a u8 is : {}", 1000 as u8);
//println!(" 232 as a i8 is : {}", 232 as i8);
literals();
}
fn literals() {
//类型作为后缀
let x = 1u8;
let y = 2u32;
let z = 3f32;
//非固定文字,其类型取决于如何使用
let i = 1;
let f = 1.0;
//Returns the size of the pointed-to value in bytes.
println!("size of `x` in bytes: {}", std::mem::size_of_val(&x));
println!("size of `y` in bytes: {}", std::mem::size_of_val(&y));
println!("size of `z` in bytes: {}", std::mem::size_of_val(&z));
println!("size of `i` in bytes: {}", std::mem::size_of_val(&i));
println!("size of `f` in bytes: {}", std::mem::size_of_val(&f));
}