练习
cargo init
cargo run
代码如下
fn main() {
// test_if();
// test_while();
// test_loop();
test_for();
}
#[allow(dead_code)]
fn test_if() {
let age_to_drive = 16u8;
println!("Enter the persion's age:");
let myinput = &mut String::from("");
std::io::stdin().read_line(myinput).unwrap();
// "15\r\n"
println!("{:?}", myinput);
let age = myinput.replace("\r\n", "").parse::<u8>().unwrap();
if age > age_to_drive {
println!("old enough");
} else if age == age_to_drive {
println!("with one more year .");
} else {
println!("wait a bit longer, u an't old enough for a driver's license!!");
}
let drivers_license = if age >= 16 { true } else { false };
}
#[allow(dead_code)]
fn test_while() {
let age_to_drive = 16u8;
let mut current_age = 0u8;
while current_age < age_to_drive {
println!("while true waiting......");
current_age += 1;
if current_age == 6 {
break;
}
}
}
#[allow(dead_code)]
fn test_loop() {
let mut x = 1;
loop {
println!("Hello rust");
if x > 5 {
break;
}
x += 1;
}
}
fn test_for() {
let ages = [14, 18, 26, 35, 41];
let age_to_drive = 16i32;
for age in ages {
if age >= age_to_drive {
println!("great, u can drive a car now....{0}", age);
} else {
println!("u need wait a little bit more.. {0}", age);
}
}
}