Bird
Raised Fist0
Rustprogramming~10 mins

Loop construct in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Loop construct
Start
↓
Execute loop body
↓
Check condition or continue?
↓
Repeat loop body
No↓
Exit loop
The loop runs the body repeatedly until a break condition stops it.
Execution Sample
Rust
let mut count = 0;
loop {
    if count == 3 {
        break;
    }
    count += 1;
}
This Rust loop increases count from 0 until it reaches 3, then stops.
Execution Table
StepcountCondition (count == 3)ActionLoop continues?
10falsecount += 1 -> 1Yes
21falsecount += 1 -> 2Yes
32falsecount += 1 -> 3Yes
43truebreakNo
💡 At step 4, count == 3 is true, so loop breaks and stops.
Variable Tracker
VariableStartAfter 1After 2After 3Final
count01233
Key Moments - 2 Insights
Why does the loop stop when count reaches 3?
Because at step 4 in the execution_table, the condition count == 3 is true, triggering the break statement which exits the loop.
What happens if the break statement is missing?
The loop would run forever because there is no condition to stop it, as shown by the loop continuing 'Yes' in all steps except the break step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count at step 3?
A2
B3
C1
D0
💡 Hint
Check the 'count' column in the execution_table at step 3.
At which step does the loop stop running?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Loop continues?' column to find when it says 'No'.
If the condition was changed to count == 5, when would the loop break?
AAt step 3
BAt step 4
CAt step 6
DIt would never break
💡 Hint
The loop breaks when count equals the condition; count increments by 1 each step.
Concept Snapshot
Rust loop construct syntax:
loop {
    // code
    if condition {
        break;
    }
}
The loop runs repeatedly until break is called.
Use break to stop the loop when a condition is met.
Full Transcript
This example shows a Rust loop that starts with count at 0. Each loop step checks if count equals 3. If not, it adds 1 to count and repeats. When count reaches 3, the break statement stops the loop. The execution_table tracks each step's count value, condition check, and whether the loop continues. The variable_tracker shows how count changes from 0 to 3. Key moments explain why the loop stops and what happens without break. The visual quiz tests understanding of count values and loop stopping points. This helps beginners see how Rust loops run step-by-step.

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

  1. Step 1: Understand the loop keyword

    The loop keyword in Rust creates a loop that repeats forever unless stopped.
  2. Step 2: Identify how to stop the loop

    To stop a loop, you must use break inside it; otherwise, it runs infinitely.
  3. Final Answer:

    It creates an infinite loop that runs until break is called. -> Option B
  4. Quick Check:

    loop = infinite loop until break [OK]
Hint: Remember: loop runs forever unless broken [OK]
Common Mistakes:
  • Thinking loop runs a fixed number of times
  • Confusing loop with for or while
  • Assuming loop stops automatically
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

  1. Step 1: Check Rust syntax for infinite loops

    Rust uses the keyword loop to create an infinite loop.
  2. 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.
  3. Final Answer:

    loop { println!("Hello"); } -> Option A
  4. 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

  1. 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.
  2. Step 2: List printed values

    Only 1 and 2 are printed before the break stops the loop.
  3. Final Answer:

    1 2 -> Option A
  4. 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

  1. Step 1: Check loop control variable

    The variable i is initialized but never changed inside the loop.
  2. Step 2: Understand loop behavior

    Since i never increments, i == 5 is never true, so break is never reached, causing an infinite loop.
  3. Final Answer:

    Variable 'i' is not incremented, causing an infinite loop. -> Option D
  4. 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

  1. 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.
  2. Step 2: Use break to stop after 5

    Increment counter each loop. When counter > 5, use break to exit loop.
  3. Step 3: Result is sum of 1 to 5

    This method correctly sums numbers 1+2+3+4+5 = 15.
  4. Final Answer:

    Use a loop with a counter and break when counter > 5, adding each to sum. -> Option C
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard