Bird
Raised Fist0
Rustprogramming~10 mins

Array basics in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array basics
Declare array with fixed size
↓
Initialize elements with values
↓
Access elements by index
↓
Modify elements by index
↓
Use array elements in expressions
↓
End
This flow shows how an array is created, accessed, modified, and used step-by-step.
Execution Sample
Rust
fn main() {
    let mut arr = [10, 20, 30];
    println!("First element: {}", arr[0]);
    arr[1] = 50;
    println!("Modified array: {:?}", arr);
}
This code creates an array, prints the first element, changes the second element, then prints the whole array.
Execution Table
StepActionArray StateIndex AccessedValue Read/WrittenOutput
1Declare and initialize array[10, 20, 30]---
2Read element at index 0[10, 20, 30]010 (read)First element: 10
3Modify element at index 1[10, 50, 30]150 (written)-
4Print entire array[10, 50, 30]--Modified array: [10, 50, 30]
5End of program[10, 50, 30]---
💡 Program ends after printing the modified array.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
arruninitialized[10, 20, 30][10, 50, 30][10, 50, 30]
Key Moments - 3 Insights
Why do we use square brackets [ ] to access array elements?
Square brackets [ ] are used to specify the index of the element we want to read or change, as shown in steps 2 and 3 of the execution table.
Why must the array be declared as mutable to change elements?
In Rust, arrays are immutable by default. To modify elements (step 3), the array must be declared with 'mut' so the program allows changes.
What happens if we try to access an index outside the array size?
Rust will panic at runtime if an index is out of bounds. This is not shown here but is important to avoid errors.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of arr after step 3?
A[10, 50, 30]
B[50, 20, 30]
C[10, 20, 30]
D[10, 20, 50]
💡 Hint
Check the 'Array State' column at step 3 in the execution table.
At which step is the first element of the array read?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for 'Read element at index 0' in the 'Action' column.
If we remove 'mut' from the array declaration, what happens at step 3?
AThe program panics at runtime.
BThe compiler gives an error about mutability.
CThe program compiles and runs normally.
DThe array elements become zero.
💡 Hint
Rust requires 'mut' to modify variables; see step 3 action about modification.
Concept Snapshot
Array basics in Rust:
- Declare with fixed size and type: let arr = [val1, val2, val3];
- Use 'mut' to allow changes: let mut arr = [...];
- Access elements by index: arr[0], arr[1], ...
- Indexing starts at 0
- Out-of-bounds access causes runtime panic
Full Transcript
This lesson shows how to create and use arrays in Rust. First, we declare an array with fixed size and initial values. Then we access elements by their index using square brackets. We can change elements only if the array is declared mutable with 'mut'. The example code prints the first element, modifies the second, and prints the whole array. The execution table tracks each step, showing array state and outputs. Key points include indexing starting at zero and the need for mutability to modify elements. Accessing outside the array size causes a runtime error.

Practice

(1/5)
1. What is true about arrays in Rust?
easy
A. Their size can change after creation.
B. They store multiple values of the same type in a fixed-size list.
C. They can store values of different types together.
D. They do not allow access by index.

Solution

  1. Step 1: Understand array type restrictions

    Arrays in Rust hold values all of the same type, ensuring type safety.
  2. Step 2: Understand array size behavior

    Arrays have a fixed size that cannot be changed after creation.
  3. Final Answer:

    They store multiple values of the same type in a fixed-size list. -> Option B
  4. Quick Check:

    Array = fixed size + same type [OK]
Hint: Remember: Rust arrays are fixed size and single type [OK]
Common Mistakes:
  • Thinking arrays can hold mixed types
  • Assuming arrays can grow or shrink
  • Believing arrays don't use indexes
2. Which of the following is the correct way to declare an array of 3 integers in Rust?
easy
A. let arr = [1, 2, 3];
B. let arr = (1, 2, 3);
C. let arr = {1, 2, 3};
D. let arr = <1, 2, 3>;

Solution

  1. Step 1: Identify Rust array syntax

    Arrays use square brackets [] with comma-separated values.
  2. Step 2: Check other options

    Parentheses () create tuples, curly braces {} are blocks, angle brackets <> are not valid here.
  3. Final Answer:

    let arr = [1, 2, 3]; -> Option A
  4. Quick Check:

    Array syntax uses [] [OK]
Hint: Arrays use square brackets [] with commas [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Confusing arrays with tuples
  • Using invalid symbols like {} or <>
3. What will be the output of this Rust code?
let arr = [10, 20, 30, 40];
println!("{}", arr[2]);
medium
A. Index out of bounds error
B. 20
C. 40
D. 30

Solution

  1. Step 1: Understand array indexing

    Rust arrays start indexing at 0, so arr[2] is the third element.
  2. Step 2: Identify the element at index 2

    The elements are [10, 20, 30, 40], so arr[2] is 30.
  3. Final Answer:

    30 -> Option D
  4. Quick Check:

    Index 2 = 30 [OK]
Hint: Count from zero to find the correct index [OK]
Common Mistakes:
  • Starting count at 1 instead of 0
  • Confusing element positions
  • Expecting runtime error for valid index
4. Find the error in this Rust code snippet:
let arr = [1, 2, 3];
println!("{}", arr[3]);
medium
A. No error, prints 3
B. Type mismatch error
C. Index out of bounds error at runtime
D. Syntax error due to missing semicolon

Solution

  1. Step 1: Check array length and index used

    The array has 3 elements with indexes 0, 1, 2. Index 3 is invalid.
  2. Step 2: Understand Rust runtime behavior

    Accessing arr[3] causes a runtime panic due to index out of bounds.
  3. Final Answer:

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

    Invalid index causes runtime panic [OK]
Hint: Check if index is less than array length [OK]
Common Mistakes:
  • Assuming index 3 is valid for 3-element array
  • Confusing syntax error with runtime error
  • Ignoring Rust's panic on invalid index
5. You want to create an array of 5 zeros in Rust. Which code correctly does this?
hard
A. let arr = [0; 5];
B. let arr = [0, 5];
C. let arr = [5; 0];
D. let arr = [0] * 5;

Solution

  1. Step 1: Understand Rust array repetition syntax

    The syntax [value; count] creates an array repeating value count times.
  2. Step 2: Check each option

    let arr = [0; 5]; uses [0; 5], which creates 5 zeros. Others are invalid or create wrong arrays.
  3. Final Answer:

    let arr = [0; 5]; -> Option A
  4. Quick Check:

    Use [value; count] to repeat values [OK]
Hint: Use [value; count] to make repeated arrays [OK]
Common Mistakes:
  • Using commas instead of semicolon for repetition
  • Confusing multiplication syntax with arrays
  • Reversing value and count in repetition
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