Rust 入门 - 控制流
2021-05-26 本文已影响0人
Lee_dev
if
let a = 8;
if a == 8 {
println!("a = 8");
} else {
println!("a != 8");
}
if 表达式
let sw = true;
let a = if sw { 6 } else { 8 };
println!("a = {}", a);
loop
let mut index = 0;
loop {
if index == 10 {
break;
}
index += 1;
}
println!("index = {}", index);
loop 表达式
let mut index = 0;
let res = loop {
if index == 10 {
break index * 10;
}
index += 1;
};
println!("index = {}", index);
println!("res = {}", res);
while
let mut index = 0;
while index < 10 {
index += 1;
println!("index = {}", index);
}
println!("index = {}", index);
数组遍历
let nums: [u32; 5] = [1, 2, 3, 4, 5];
for n in &nums {
println!("n = {}", n);
}
for n in nums.iter() {
println!("n = {}", n);
}