Complete the code to create a while loop that counts down from 3 to 1.
let mut count = 3; while [1] { println!("{}", count); count -= 1; }
The condition count > 0 keeps the loop running while count is positive, counting down from 3 to 1.
Complete the code to print numbers from 1 to 5 using a while loop.
let mut num = 1; while [1] { println!("{}", num); num += 1; }
num < 5 which stops before printing 5.num == 5 which runs only once.The condition num <= 5 ensures the loop prints numbers 1 through 5 inclusive.
Fix the error in the while loop condition to avoid an infinite loop.
let mut x = 0; while [1] { println!("{}", x); x += 1; if x == 3 { break; } }
x < 3 would also work but the task asks to fix the error with break.Using true creates an infinite loop, but the break statement stops it when x == 3.
Fill both blanks to create a while loop that prints even numbers less than 10.
let mut n = 0; while [1] { println!("{}", n); n [2] 2; }
The loop runs while n < 10 and increments n by 2 each time to print even numbers.
Fill all three blanks to create a while loop that collects numbers less than 5 into a vector.
let mut v = Vec::new(); let mut i = 0; while [1] { v.push([2]); i [3] 1; }
The loop runs while i < 5, pushes i into the vector, and increments i by 1 each time.