讲解内容
fn main() {
//if
let y = 1;
if y == 1 {
println!("y = 1");
}
//if-else
if y == 1 {
println!("y = 1");
} else {
println!("y != 1");
}
//if - else if - else
if y == 0 {
println!("y = 0");
} else if y == 1 {
println!("y = 1");
} else {
println!("y != 1");
}
//在let中使用if
let condition = true;
let x = if condition {
5
} else {
6
//"six" //error
};
println!("x = {}", x);
//loop
//loop {
// println!("in loop");
//}
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 2;
}
};
println!("The result is {}", result);
//while
let mut i = 0;
while i != 10 {
i = i+1;
}
println!("i = {}", i);
//for
let arr = [1, 2, 3, 4];
for element in arr.iter() {
println!(" element = {}", element);
}
println!("Hello, world!");
}
领取专属 10元无门槛券
私享最新 技术干货