A while loop in Rust repeats a block of code as long as a given condition is true. It checks the condition before each repetition.
Click to reveal answer
beginner
How do you write a basic while loop that counts from 1 to 5 in Rust?
You can write it like this:<br><pre>let mut count = 1;
while count <= 5 {
println!("{}", count);
count += 1;
}</pre>
Click to reveal answer
beginner
What happens if the condition in a while loop is initially false?
The code inside the while loop will not run at all because the condition is checked before the loop starts.
Click to reveal answer
intermediate
How can you stop an infinite while loop in Rust?
You can stop it by using a break statement inside the loop when a certain condition is met.
Click to reveal answer
beginner
Why do you need to declare the loop variable as mut in a while loop?
Because the variable changes its value inside the loop, it must be mutable (mut) so Rust allows updating it.
Click to reveal answer
What keyword starts a while loop in Rust?
Arepeat
Bwhile
Cfor
Dloop
✗ Incorrect
The keyword while is used to start a while loop in Rust.
What happens if the while loop condition is false at the start?
AThe loop does not run at all
BThe loop runs once
CThe loop runs infinitely
DThe program crashes
✗ Incorrect
The while loop checks the condition before running. If false, it skips the loop.
How do you stop a while loop early inside the loop?
Aexit
Bcontinue
Cstop
Dbreak
✗ Incorrect
The break keyword stops the loop immediately.
Why must the loop counter be declared with mut?
ABecause it changes value inside the loop
BBecause Rust requires all variables to be mutable
CBecause it is a constant
DBecause it is a string
✗ Incorrect
The counter changes each time the loop runs, so it must be mutable.
Which of these is a correct while loop condition to count from 1 to 3?
Awhile count == 3
Bwhile count < 3
Cwhile count <= 3
Dwhile count > 3
✗ Incorrect
Using while count <= 3 counts 1, 2, and 3.
Explain how a while loop works in Rust and give a simple example.
Think about counting numbers using a variable that changes.
You got /4 concepts.
Describe how to stop an infinite while loop in Rust.
Imagine you want to stop the loop when a number reaches 10.
You got /3 concepts.
Practice
(1/5)
1.
What does a while loop do in Rust?
easy
A. Stops the program immediately
B. Runs code only once
C. Repeats code a fixed number of times
D. Repeats code as long as a condition is true
Solution
Step 1: Understand the purpose of a while loop
A while loop runs repeatedly while its condition is true.
Step 2: Compare options with this behavior
Only Repeats code as long as a condition is true describes repeating code while a condition is true.
Final Answer:
Repeats code as long as a condition is true -> Option D
Quick Check:
while loop = repeat while true [OK]
Hint: While loops run while condition is true [OK]
Common Mistakes:
Confusing while loop with for loop
Thinking while loop runs fixed times
Believing while loop runs once only
2.
Which of the following is the correct syntax for a while loop in Rust?
??? x < 5 {
println!("{}", x);
x += 1;
}
easy
A. while
B. loop
C. for
D. if
Solution
Step 1: Identify the keyword for a while loop
The keyword to start a while loop is while.
Step 2: Check other keywords
loop is an infinite loop, for is for fixed iteration, if is conditional, not looping.
Final Answer:
while -> Option A
Quick Check:
while keyword starts while loop [OK]
Hint: While loops start with 'while' keyword [OK]
Common Mistakes:
Using 'loop' instead of 'while'
Using 'for' for while loops
Using 'if' instead of loop keyword
3.
What will be the output of this Rust code?
let mut count = 1;
while count <= 3 {
println!("{}", count);
count += 1;
}
medium
A. No output
B. 1 2 3 4
C. 1 2 3
D. 0 1 2
Solution
Step 1: Analyze the loop condition and variable
The loop runs while count is less than or equal to 3, starting at 1.
Step 2: Trace the output values
It prints 1, then 2, then 3, then stops because count becomes 4.
Final Answer:
1 2 3 -> Option C
Quick Check:
Prints 1 to 3 inclusive [OK]
Hint: Count from 1 to 3 prints 1 2 3 [OK]
Common Mistakes:
Thinking it prints 4
Starting count at 0
Assuming no output
4.
Find the error in this Rust code using a while loop:
let mut i = 0;
while i < 5 {
println!("{}", i);
}
medium
A. Wrong comparison operator in while condition
B. Missing increment of 'i' inside the loop
C. 'i' should be immutable, not mutable
D. println! macro syntax is incorrect
Solution
Step 1: Check loop variable update
The variable 'i' is never increased inside the loop, so the condition never becomes false.
Step 2: Understand effect of missing increment
Without increment, the loop runs forever (infinite loop).
Final Answer:
Missing increment of 'i' inside the loop -> Option B
Quick Check:
Increment needed to avoid infinite loop [OK]
Hint: Always update loop variable inside while loop [OK]
Common Mistakes:
Forgetting to increment loop variable
Using wrong operator but code still compiles
Misunderstanding mutability requirement
5.
Write a while loop in Rust that prints all even numbers from 2 to 10 inclusive.
Which code snippet correctly does this?
???
hard
A. let mut n = 2;
while n <= 10 {
println!("{}", n);
n += 2;
}
B. let mut n = 1;
while n < 10 {
if n % 2 == 0 {
println!("{}", n);
}
n += 1;
}
C. let mut n = 2;
while n < 10 {
println!("{}", n);
n += 2;
}
D. let mut n = 0;
while n <= 10 {
println!("{}", n);
n += 2;
}
Solution
Step 1: Check the range and values printed
We want even numbers from 2 to 10 inclusive, so start at 2 and go up to 10.
Step 2: Analyze each option
let mut n = 2;
while n <= 10 {
println!("{}", n);
n += 2;
} starts at 2, increments by 2, and stops at 10 inclusive. let mut n = 1;
while n < 10 {
if n % 2 == 0 {
println!("{}", n);
}
n += 1;
} starts at 1 and checks evenness each time but stops before 10, missing 10. let mut n = 2;
while n < 10 {
println!("{}", n);
n += 2;
} stops before 10, missing 10. let mut n = 0;
while n <= 10 {
println!("{}", n);
n += 2;
} starts at 0, which is even but outside the requested range.
Final Answer:
let mut n = 2;
while n <= 10 {
println!("{}", n);
n += 2;
} -> Option A
Quick Check:
Start 2, step 2, end 10 inclusive [OK]
Hint: Start at 2, add 2 each loop until 10 [OK]
Common Mistakes:
Starting at 0 instead of 2
Stopping before 10
Not incrementing by 2
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.