以下代码简单介绍了 Rust 的基础数据类型
fn main() {
// bool
let is_true: bool = true;
println!("is_true = {}", is_true);
let is_false = false;
println!("is_false = {}", is_false);
// char 在 Rust 里面,char 是 32 位的
let a = 'a';
println!("a = {}", a);
let b = '你';
println!("b = {}", b);
// i8, i16, i32, i64, u8, u16, u32, u64, f32, f64
let c: i8 = -111;
println!("c = {}", c);
let d: f32 = 0.0009;
println!("d = {}", d);
// 自适应类型 isize, usize
println!("max = {}", usize::max_value());
println!("Hello, world!");
// 数组
// [Type: size], size 也是数组类型的一部分
let arr: [u32; 3] = [1, 2, 3];
println!("arr[0] = {}", arr[0]);
show(arr);
// 元组
let tup: (i32, f32, char) = (-3, 3.69, '好');
println!("---------------------");
println!("{}", tup.0);
println!("{}", tup.1);
println!("{}", tup.2);
println!("---------------------");
let (x, y, z) = tup;
println!("{}", x);
println!("{}", y);
println!("{}", z);
}
fn show(arr:[u32; 3]) {
println!("---------------------");
for i in &arr {
println!("{}", i);
}
println!("---------------------");
}
输入 cargo run
,输出如下
is_true = true
is_false = false
a = a
b = 你
c = -111
d = 0.0009
max = 18446744073709551615
Hello, world!
arr[0] = 1
---------------------
1
2
3
---------------------
---------------------
-3
3.69
好
---------------------
-3
3.69
好