- 1.if
let x:i32 = 5;
let y:i32 = if x == 5 {9} else {10};
println!("{}", y);//9
- 2.loop:无限循环
loop{
println!("forever...");
}
- 3.while
let mut index = 0;
while index < 5 {
println!("{}", index);
index++;
}
- 4.for
for variable in expression {
}
for num in 0..10 {
println!("{}", num);//0~9
}
- 5.enumerate()
for (index, value) in (0..10).enumerate() {
println!("index = {}, value = {}", index, value);
}
//index = 0, value = 0...
再看一个例子:
let lines = "hello\nworld".lines();
for(linenumber, line) in lines.enumerate() {
println!("{}: {}", linenumber, line);
}
//0: hello 1: world
- 6.循环标签
fn main(){
'out for x in 0..10 {
'inn for y in 0..10 {
if x % 2 == 0 {continue 'out;}
if y % 2 == 0 {continue 'inn;}
println!("{} {}", x, y);
}
}
}