Files
2021-03-14 10:46:00 -05:00

53 lines
826 B
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Rust has three kinds of loops: loop, while, and for. Lets try each one + recursive.
fn main() {
countdown_loop(5);
println!("------");
countdown_while(5);
println!("------");
countdown_for(5);
println!("------");
countdown_recursive(5);
}
fn countdown_loop(count: usize) {
let mut i = count;
loop {
println!("{}", i);
i -= 1;
if i <= 0 {
break;
}
}
}
fn countdown_while(count: usize) {
let mut i = count;
while i > 0 {
println!("{}", i);
i -= 1;
}
}
fn countdown_for(count: usize) {
for i in (1..count + 1).rev() {
println!("{}", i);
}
}
fn countdown_recursive(count: usize) {
println!("{}", count);
if count > 1 {
return countdown_recursive(count - 1);
}
}