Last active
May 3, 2024 13:00
-
-
Save georgematos/289dbc413bef0d6c39c7568a6d297ae7 to your computer and use it in GitHub Desktop.
Rust - loop vs while vs for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let mut count = 3; | |
loop { | |
if count > 0 { | |
println!("{count}!"); | |
count -= 1; | |
} else { | |
break | |
} | |
} | |
println!("LIFTOFF!"); | |
let mut count = 3; | |
while count > 0 { | |
println!("{count}!"); | |
count -= 1; | |
} | |
println!("LIFTOFF!"); | |
for number in (1..4).rev() { | |
println!("{number}!") | |
} | |
println!("LIFTOFF!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment