Loops help repeat actions many times without writing the same code again and again.
Loop execution flow in Rust
Introduction
Syntax
Rust
loop {
// code to repeat
if condition {
break;
}
}loop creates an infinite loop unless you use break to stop it.
You can use continue to skip the rest of the current loop and start the next cycle.
Examples
break.Rust
let mut count = 0; loop { count += 1; println!("Count is {}", count); if count == 5 { break; } }
continue.Rust
let mut n = 0; loop { n += 1; if n % 2 == 0 { continue; // skip even numbers } println!("Odd number: {}", n); if n > 10 { break; } }
Sample Program
This program runs a loop that prints the current iteration number. When it reaches 3, it prints a message and stops the loop.
Rust
fn main() {
let mut i = 1;
loop {
println!("Loop iteration: {}", i);
if i == 3 {
println!("Reached 3, stopping loop.");
break;
}
i += 1;
}
}Important Notes
Use break to stop a loop when you want to exit early.
Use continue to skip the rest of the current loop cycle and start the next one.
Infinite loops can crash your program if you forget to use break.
Summary
Loops repeat code until you tell them to stop.
break stops the loop completely.
continue skips to the next loop cycle.
Practice
1. What does the
break statement do inside a Rust loop?easy
Solution
Step 1: Understand the role of
Thebreakin loopsbreakstatement immediately stops the loop and exits it.Step 2: Compare with other loop controls
continueskips to the next iteration, butbreakends the loop entirely.Final Answer:
It stops the loop immediately and exits it. -> Option DQuick Check:
breakstops loop = C [OK]
Hint: Remember: break = stop loop, continue = skip iteration [OK]
Common Mistakes:
- Confusing break with continue
- Thinking break pauses instead of stops
- Assuming break restarts the loop
2. Which of the following is the correct syntax to skip the current iteration in a Rust
for loop?easy
Solution
Step 1: Identify the keyword to skip iteration
In Rust,continue;skips the rest of the current loop iteration and moves to the next.Step 2: Check other options for correctness
break;stops the loop,skip;andnext;are not valid Rust keywords for this purpose.Final Answer:
continue; -> Option BQuick Check:
Skip iteration = continue [OK]
Hint: Use continue to skip current loop iteration [OK]
Common Mistakes:
- Using break instead of continue
- Using non-existent keywords like skip or next
- Omitting the semicolon
3. What will be the output of this Rust code?
let mut count = 0;
loop {
count += 1;
if count == 3 {
break;
}
println!("{}", count);
}medium
Solution
Step 1: Trace the loop iterations and print statements
Loop increments count from 0 to 1, prints 1; then 2, prints 2; when count is 3, break stops loop before printing.Step 2: Determine printed values
Only 1 and 2 are printed before break stops the loop.Final Answer:
1 2 -> Option CQuick Check:
Prints before break = 1 2 [OK]
Hint: Break stops before printing count 3 [OK]
Common Mistakes:
- Assuming count 3 is printed
- Starting count from 1 instead of 0
- Confusing break and continue effects
4. Identify the error in this Rust loop code:
for i in 1..5 {
if i == 3 {
continue
}
println!("{}", i);
}medium
Solution
Step 1: Check syntax of continue statement
In Rust, statements likecontinuerequire a semicolon to end the statement.Step 2: Verify other parts of the code
The range1..5andprintln!usage are correct;continueis valid inside for loops.Final Answer:
Missing semicolon after continue -> Option AQuick Check:
Statements need semicolon = A [OK]
Hint: Add semicolon after continue to fix syntax [OK]
Common Mistakes:
- Forgetting semicolon after continue
- Thinking continue is invalid in for loops
- Misreading range syntax
5. Given this Rust code, what will be the final value of
sum?
let mut sum = 0;
for i in 1..6 {
if i % 2 == 0 {
continue;
}
if i == 5 {
break;
}
sum += i;
}hard
Solution
Step 1: Analyze loop iterations and conditions
Loop runs i from 1 to 5 (exclusive 6). For even i, continue skips adding. For i == 5, break stops loop before adding.Step 2: Calculate sum for valid i values
i=1 (odd, add 1), i=2 (even, skip), i=3 (odd, add 3), i=4 (even, skip), i=5 (break before add). sum = 1 + 3 = 4.Final Answer:
4 -> Option AQuick Check:
Sum odd before 5 = 4 [OK]
Hint: Add only odd numbers before 5, skip evens [OK]
Common Mistakes:
- Adding even numbers mistakenly
- Including 5 in sum despite break
- Confusing continue and break effects
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
