转义输出:
rust
复制代码
fn main()
{
let a = 12;
println!("a = {{{}}}", a);
}
//a = {12}
数据类型
rust
复制代码
let is_true: bool = true;
println!("is_true = {}", is_true);
let is_false = false;
println!("is_false = {}, {}", is_false, is_true);
//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());
3. 函数
rust
复制代码
fn other_fun2(a: i32, b: i32) -> i32 {
let result = a + b;
return result;
}
fn other_fun3(a: i32, b: i32) -> i32 {
//let result = a + b;
//result
a + b
}
fn main() {
let a: i32 = -1;
let b: u32 = 2;
let c: i32 = 9;
let r: i32 = other_fun2(a, c);
println!("r = {}", r);
let r2: i32 = other_fun3(a, c);
println!("r2 = {}", r2);
//语句是执行一些操作,但是不返回值的指令
//let y = 1; //语句,不返回值
//let x = (let y = 1);
//表达式会计算一些值
let y = {
let x = 1;
//x + 1;
x + 1
};
println!("y = {}", y);
println!("Hello, world!");
}
4. 循环体
rust
复制代码
fn main() {
//if
let y = 0;
if y == 1 {
println!("y = 1");
}
//if-else
if y == 1 {
println!("y = 1");
} else {
println!("y != 1");
}
//if - else if - else
println!("++++++++++++");
let y = 2;
if y == 1 {
println!("y = 1");
} else if y == 0 {
println!("y = 0");
} else if y == 2 {
println!("y = 2");
} else {
println!("other");
}
//let中使用if
let condition = true;
let x = if condition {
5
} else {
6
//"six" //error
};
println!("x = {}", x);
//loop
let mut counter = 0;
loop {
println!("in loop");
if counter == 10 {
break;
}
//counter = counter + 1;
counter += 1;
}
let result = loop {
counter += 1;
if counter == 20 {
break counter*2;
}
};
println!("result = {}", result);
//while
let mut i = 0;
while i != 10 {
i += 1;
}
println!("i = {}", i);
//for
println!("+++++++++++++++");
let arr:[u32; 5] = [1, 2, 3, 4, 5];
//for element in arr.iter() {
for element in &arr {
println!("element = {}", element);
}
println!("Hello, world!");
}