基础类型有
- integer
- float
- boolean
- char
0x01 integer
- 类型定义
// 分为有符号 和 无符号整形
let a1: i8;
let a2: i16;
let a3: i32;
let a4: i64;
let a5: i128;
let a6: isize;
let b1: u8;
let b2: u16;
let b3: u32;
let b4: u64;
let b5: u128;
let b6: usize;
- 值定义
包含了八进制和二进制的值定义, 这个可以赞一下
let c1 = 19_80; // 10
let c2 = 0xff; // 16
let c3 = 0o77; // 8
let c4 = 0b0001_0001; // 2
let c5 = b'A'; // byte
0x02 float
let f1: f32 = 2.0;
let f2: f64 = 3.0;
0x03 bool
let g1 = true;
let g2: bool = false;
0x04 char
let h = 'z';
let h2 = 'Z';
组合类型
0x05 tuple
let tup: (i32, f32, u8, bool) = (500, 6.4, 1, true);
let (x, y, z, b) = tup;
0x06 array
每一个元素必需使用相同的类型
let arr = [1, 2, 3, 4, 5];
let weekdays: [&str; 3] = ["mon", "tue", "wedn"];
let blankArr = [3; 5]; // = let blankArr = [3,3,3,3,3]
let ele1 = arr[0];
let ele2 = arr[1];
- array 的两个遍历方法
let s = String::from("hello");
let x = s.as_bytes();
for (i, &item) in x.iter().enumerate() {
println!("{} - {}", i, item);
}
println!("{}", "-------");
for t in x.iter() {
println!("{}", t);
}
网友评论