keyword
Rust while: repeat while a condition holds
Use `while` when repetition depends on a condition that can change. The condition is checked before each iteration, so the body may execute zero times. Expected output: 3 2 1 Why: The counter decreases until the next condition check is false. Common mistake: Forgetting to update the counter can make a loop run indefinitely.
Formal Syntax & Structure
rust syntaxrust
while condition { statements }Working Example
Example Snippetrust
fn main() { let mut remaining = 3; while remaining > 0 { println!("{remaining}"); remaining -= 1; }}