变量声明
let foo = 5; // imutable
let mut bar = 5; // mutable
打印输出Println!
println!(“x = {}, y = {}”, x, y);
数据类型
整形
let guess: u32 = “42”.parse().expect(“Not a number!”);
浮点数
主要是32位和64位,默认64
let x = 2.0; // f64
let y: f32 = 3.0; // f32
布尔类型
let t = true;
let f: bool = false; // with explicit type annotation
Turple
fn main() {
let tup: (i32, f64, u8) = (500, 6.4, 1);
let (x, y, z) = tup; //解
let x: (i32, f64, u8) = (500, 6.4, 1);
//下标0, 1, 2访问
let five_hundred = x.0;
let six_point_four = x.1;
let one = x.2;
}
Array
let a = [1, 2, 3, 4, 5];
let months = [“January”, “February”, “March”, “April”, “May”, “June”, “July”,
“August”, “September”, “October”, “November”, “December”];
let a = [1, 2, 3, 4, 5];
let first = a[0];
let second = a[1];
Char
let ch = b’a’
let ch = ‘😻’
函数定义和函数调用
fn main() -> ! {
println!(“Hello world!”);
}
控制流
IF ELSE
if number < 5 {
println!(“condition was true”);
} else {
println!(“condition was false”);
}
Loop
loop {
println!(“Again!”);
}
while index < 5 {
println!(“the value is: {}”, a[index]);
index += 1;
}
let a = [10, 20, 30, 40, 50];
for element in a.iter() {
println!(“the value is: {}”, element);
}