Bird
Raised Fist0
DSA Rustprogramming~10 mins

Array Traversal Patterns in DSA Rust - Execution Trace

Choose your learning style10 modes available
Concept Flow - Array Traversal Patterns
Start at index 0
↓
Check: index < array length?
No→Stop traversal
Yes↓
Access element at current index
↓
Process element (e.g., print)
↓
Increment index by 1
↩Back to Check
This flow shows how to move through an array from the first element to the last by checking the index, accessing the element, processing it, and then moving to the next index until the end.
Execution Sample
DSA Rust
let arr = [10, 20, 30, 40];
let mut i = 0;
while i < arr.len() {
    println!("{}", arr[i]);
    i += 1;
}
This code prints each element of the array one by one by moving through the array indices from 0 to length-1.
Execution Table
StepOperationIndex (i)Condition (i < arr.len())Element AccessedActionOutputArray State
1Initialize i00 < 4 = Truearr[0] = 10Print 1010[10, 20, 30, 40]
2Increment i11 < 4 = Truearr[1] = 20Print 2020[10, 20, 30, 40]
3Increment i22 < 4 = Truearr[2] = 30Print 3030[10, 20, 30, 40]
4Increment i33 < 4 = Truearr[3] = 40Print 4040[10, 20, 30, 40]
5Increment i44 < 4 = FalseNo accessStop loop[10, 20, 30, 40]
💡 Index i reaches 4, condition i < arr.len() becomes false, stopping traversal.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5 (End)
i012344
Output"""10""10 20""10 20 30""10 20 30 40""10 20 30 40"
Key Moments - 3 Insights
Why does the loop stop when i equals the array length?
Because the condition i < arr.len() becomes false at step 5 (see execution_table row 5), preventing access beyond the array bounds.
What happens if we try to access arr[i] when i equals arr.len()?
Accessing arr[i] at i == arr.len() would cause an error because it is outside the array range; the loop condition prevents this by stopping before that.
Why do we start the index i at 0 instead of 1?
Arrays in Rust are zero-indexed, so the first element is at index 0, as shown in step 1 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i at step 3?
A1
B2
C3
D4
💡 Hint
Check the 'Index (i)' column in execution_table row 3.
At which step does the condition i < arr.len() become false?
AStep 5
BStep 4
CStep 3
DStep 2
💡 Hint
Look at the 'Condition (i < arr.len())' column in execution_table.
If we start i at 1 instead of 0, what element is printed first?
A10
B30
C20
D40
💡 Hint
Refer to the 'Element Accessed' column and index values in execution_table.
Concept Snapshot
Array Traversal Pattern:
- Start index at 0
- Loop while index < array length
- Access element at current index
- Process element (e.g., print)
- Increment index by 1
- Stop when index reaches array length
Full Transcript
This visualization shows how to traverse an array by starting at index zero and moving forward one element at a time. At each step, the code checks if the current index is less than the array length to avoid going out of bounds. It accesses the element at the current index, processes it by printing, then increments the index. The loop stops when the index equals the array length, ensuring safe access. The execution table tracks each step, showing index values, conditions, accessed elements, and outputs. Key moments clarify why the loop stops at the array length and why indexing starts at zero. The quiz tests understanding of index values and loop conditions.

Practice

(1/5)
1. What does the following Rust code snippet do?
for i in 0..5 { println!("{}", i); }
easy
A. Prints numbers from 1 to 4
B. Prints numbers from 1 to 5
C. Prints numbers from 0 to 5
D. Prints numbers from 0 to 4

Solution

  1. Step 1: Understand the range syntax 0..5

    The range 0..5 in Rust includes numbers starting at 0 up to but not including 5.
  2. Step 2: Trace the loop iterations

    The loop variable i takes values 0, 1, 2, 3, and 4, printing each number.
  3. Final Answer:

    Prints numbers from 0 to 4 -> Option D
  4. Quick Check:

    Range 0..5 excludes 5, so output is 0 to 4 [OK]
Hint: Remember Rust ranges exclude the end number [OK]
Common Mistakes:
  • Thinking 0..5 includes 5
  • Confusing 0..5 with 0..=5
  • Assuming loop starts at 1
2. Which of the following is the correct syntax to iterate over the indices of an array arr in Rust?
easy
A. for i in 0..arr.len() { println!("{}", arr[i]); }
B. for i in 0..arr { println!("{}", i); }
C. for i in arr { println!("{}", i); }
D. for i in arr.len() { println!("{}", i); }

Solution

  1. Step 1: Check valid iteration over array indices

    Rust arrays have a length method .len(), so 0..arr.len() gives valid indices.
  2. Step 2: Access elements by index

    Using arr[i] inside the loop accesses each element correctly.
  3. Final Answer:

    for i in 0..arr.len() { println!("{}", arr[i]); } -> Option A
  4. Quick Check:

    Use 0..arr.len() for index-based iteration [OK]
Hint: Use arr.len() for array length, not arr alone [OK]
Common Mistakes:
  • Using 0..arr instead of 0..arr.len()
  • Trying to iterate directly over arr without reference
  • Using arr.len() without range syntax
3. What is the output of this Rust code?
let arr = [10, 20, 30];
for i in (0..arr.len()).rev() {
    print!("{} ", arr[i]);
}
medium
A. 10 20 30
B. 0 1 2
C. 30 20 10
D. Compilation error

Solution

  1. Step 1: Understand the range and reverse

    The range 0..arr.len() is 0..3, which is 0,1,2. Using .rev() reverses it to 2,1,0.
  2. Step 2: Print elements in reverse order

    arr[2] = 30, arr[1] = 20, arr[0] = 10, so output is "30 20 10 "
  3. Final Answer:

    30 20 10 -> Option C
  4. Quick Check:

    Reverse iteration prints elements backward [OK]
Hint: Use .rev() to reverse range iteration [OK]
Common Mistakes:
  • Forgetting .rev() reverses the order
  • Printing indices instead of array values
  • Confusing range end with inclusive
4. Identify the error in this Rust code snippet that tries to print all elements of an array arr:
let arr = [1, 2, 3];
for i in 0..arr.len() {
    println!("{}", arr[i+1]);
}
medium
A. Syntax error due to missing semicolon
B. Index out of bounds error at runtime
C. No error, prints all elements correctly
D. Compilation error due to type mismatch

Solution

  1. Step 1: Analyze the loop indices and access

    The loop runs i from 0 to 2 (arr.len()=3). Accessing arr[i+1] means indices 1,2,3.
  2. Step 2: Identify out of bounds access

    arr[3] does not exist, causing runtime panic (index out of bounds).
  3. Final Answer:

    Index out of bounds error at runtime -> Option B
  4. Quick Check:

    Accessing arr[i+1] exceeds array length [OK]
Hint: Check array indices carefully to avoid out of bounds [OK]
Common Mistakes:
  • Assuming arr[i+1] is safe for all i
  • Ignoring runtime panics
  • Confusing compile-time and runtime errors
5. Given a Rust array arr of integers, which code snippet correctly prints only the even-indexed elements (0, 2, 4, ...) of the array?
hard
A. for i in (0..arr.len()).step_by(2) { println!("{}", arr[i]); }
B. for i in 0..arr.len() { if i % 2 == 1 { println!("{}", arr[i]); } }
C. for i in 0..arr.len() { println!("{}", arr[i * 2]); }
D. for i in (0..arr.len()).rev().step_by(2) { println!("{}", arr[i]); }

Solution

  1. Step 1: Understand step_by usage

    The method step_by(2) skips every second index, so 0, 2, 4, ...
  2. Step 2: Confirm correct element access

    Using arr[i] inside the loop prints elements at even indices correctly.
  3. Final Answer:

    for i in (0..arr.len()).step_by(2) { println!("{}", arr[i]); } -> Option A
  4. Quick Check:

    step_by(2) iterates over even indices [OK]
Hint: Use step_by(2) to jump every two elements [OK]
Common Mistakes:
  • Using i % 2 == 1 instead of 0 for even indices
  • Multiplying i by 2 causing out of bounds
  • Using rev() which reverses order unnecessarily
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