What is the basic syntax of an infinite loop in Rust?
In Rust, an infinite loop is created using the loop keyword followed by a block of code:
loop {
// code to repeat forever
}
Click to reveal answer
beginner
How do you exit a loop early in Rust?
You use the break keyword to exit a loop before it naturally ends. Example:
loop {
if condition {
break;
}
}
Click to reveal answer
beginner
What is the difference between while and for loops in Rust?
while loops run as long as a condition is true. for loops iterate over items in a collection or range. Example:
while x < 5 {
x += 1;
}
for i in 0..5 {
println!("{}", i);
}
Click to reveal answer
intermediate
How can a loop return a value in Rust?
A <code>loop</code> can return a value by using <code>break</code> with a value.<br>Example:<br><pre>let result = loop {
if condition {
break 42;
}
};
println!("{}", result); // prints 42</pre>
Click to reveal answer
beginner
What is the purpose of the continue keyword in Rust loops?
continue skips the rest of the current loop iteration and moves to the next one. Example:
for i in 0..5 {
if i == 2 {
continue; // skip printing 2
}
println!("{}", i);
}
Click to reveal answer
Which keyword creates an infinite loop in Rust?
Awhile
Bfor
Cloop
Drepeat
✗ Incorrect
The loop keyword creates an infinite loop in Rust.
How do you stop a loop immediately in Rust?
Abreak
Bexit
Cstop
Dreturn
✗ Incorrect
The break keyword stops a loop immediately.
What does the continue keyword do inside a loop?
AEnds the loop
BSkips to the next iteration
CRestarts the program
DPauses the loop
✗ Incorrect
continue skips the rest of the current iteration and moves to the next.
Which loop type is best to iterate over a range of numbers in Rust?
Ado-while
Bwhile
Cloop
Dfor
✗ Incorrect
The for loop is used to iterate over ranges or collections.
How can a loop return a value in Rust?
AUsing <code>break</code> with a value
BUsing <code>yield</code>
CUsing <code>continue</code> with a value
DUsing <code>return</code> inside the loop
✗ Incorrect
A loop returns a value by using break with that value.
Explain how to create and control loops in Rust, including how to exit or skip iterations.
Think about how you repeat tasks and stop or skip steps.
You got /4 concepts.
Describe how a loop can return a value in Rust and give an example.
Remember that break can send a value out of the loop.
You got /3 concepts.
Practice
(1/5)
1. What does the loop keyword do in Rust?
easy
A. It runs the loop a fixed number of times.
B. It creates an infinite loop that runs until break is called.
C. It runs the loop only once.
D. It creates a loop that runs only if a condition is true.
Solution
Step 1: Understand the loop keyword
The loop keyword in Rust creates a loop that repeats forever unless stopped.
Step 2: Identify how to stop the loop
To stop a loop, you must use break inside it; otherwise, it runs infinitely.
Final Answer:
It creates an infinite loop that runs until break is called. -> Option B
2. Which of the following is the correct syntax to create an infinite loop in Rust?
easy
A. loop { println!("Hello"); }
B. while True { println!("Hello"); }
C. for i in 0.. { println!("Hello"); }
D. repeat { println!("Hello"); }
Solution
Step 1: Check Rust syntax for infinite loops
Rust uses the keyword loop to create an infinite loop.
Step 2: Verify each option
while True { println!("Hello"); } uses while True which is invalid syntax in Rust. for i in 0.. { println!("Hello"); } uses for with an infinite range, but 0.. is not valid without an end. repeat { println!("Hello"); } uses repeat which does not exist in Rust. loop { println!("Hello"); } correctly uses loop.
Final Answer:
loop { println!("Hello"); } -> Option A
Quick Check:
Correct infinite loop syntax = loop [OK]
Hint: Use loop keyword for infinite loops in Rust [OK]
Common Mistakes:
Using while True which is invalid in Rust
Trying to use repeat which doesn't exist
Using for without a proper range
3. What will be the output of this Rust code?
let mut count = 0;
loop {
count += 1;
if count == 3 {
break;
}
println!("{}", count);
}
medium
A. 1 2
B. 0 1 2
C. 1 2 3
D. No output (infinite loop)
Solution
Step 1: Trace the loop iterations
Initially, count = 0. In the first loop, count becomes 1, which is printed. Then count becomes 2 and printed. When count becomes 3, the loop breaks before printing.
Step 2: List printed values
Only 1 and 2 are printed before the break stops the loop.
Final Answer:
1 2 -> Option A
Quick Check:
Loop prints until count == 3, so prints 1 and 2 [OK]
Hint: Check when break happens before printing [OK]
Common Mistakes:
Printing count after break condition
Starting count from 0 but expecting 0 to print
Assuming infinite loop without break
4. Identify the error in this Rust loop code:
let mut i = 0;
loop {
if i == 5 {
break;
}
println!("{}", i);
}
medium
A. 'println!' macro syntax is wrong.
B. Missing semicolon after 'break'.
C. 'loop' keyword is used incorrectly.
D. Variable 'i' is not incremented, causing an infinite loop.
Solution
Step 1: Check loop control variable
The variable i is initialized but never changed inside the loop.
Step 2: Understand loop behavior
Since i never increments, i == 5 is never true, so break is never reached, causing an infinite loop.
Final Answer:
Variable 'i' is not incremented, causing an infinite loop. -> Option D
Quick Check:
Loop needs variable increment to reach break [OK]
Hint: Always update loop variables to avoid infinite loops [OK]
Common Mistakes:
Forgetting to increment loop variable
Assuming break works without condition change
Misunderstanding loop syntax
5. How can you use a loop to sum numbers from 1 to 5 in Rust?
hard
A. Use loop with a counter but never update sum.
B. Use loop without break and add numbers forever.
C. Use a loop with a counter and break when counter > 5, adding each to sum.
D. Use loop with a counter but break immediately without adding.
Solution
Step 1: Plan the loop with counter and sum
Initialize a counter at 1 and sum at 0. Use loop to add counter to sum.
Step 2: Use break to stop after 5
Increment counter each loop. When counter > 5, use break to exit loop.
Step 3: Result is sum of 1 to 5
This method correctly sums numbers 1+2+3+4+5 = 15.
Final Answer:
Use a loop with a counter and break when counter > 5, adding each to sum. -> Option C
Quick Check:
Loop + counter + break = sum 1 to 5 [OK]
Hint: Increment counter and break after limit to sum [OK]
Common Mistakes:
Not breaking loop causing infinite sum
Not incrementing counter
Not adding counter to sum
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.