Bird
Raised Fist0
Rustprogramming~10 mins

Array slices in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Array slices
Start with array
↓
Define slice: &array[start..end
↓
Use slice as view into array
↓
Read or modify elements via slice
↓
Slice ends, original array unchanged
↓
End
Start with an array, create a slice as a view into part of it, use the slice to access or change elements, then finish with the original array intact.
Execution Sample
Rust
let arr = [10, 20, 30, 40, 50];
let slice = &arr[1..4];
println!("{}", slice[0]);
Create an array, make a slice from index 1 to 3, print the first element of the slice.
Execution Table
StepActionArray StateSlice RangeSlice ContentOutput
1Create array[10, 20, 30, 40, 50]---
2Create slice &arr[1..4][10, 20, 30, 40, 50]1..4[20, 30, 40]-
3Print slice[0][10, 20, 30, 40, 50]1..4[20, 30, 40]20
4End[10, 20, 30, 40, 50]---
💡 Slice used to access elements; program ends after printing slice[0]
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arr-[10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50]
slice--[20, 30, 40][20, 30, 40][20, 30, 40]
output---2020
Key Moments - 3 Insights
Why does the slice show elements from index 1 to 3, not including 4?
Because Rust slices use a range where the start index is inclusive and the end index is exclusive, so &arr[1..4] includes elements at indices 1, 2, and 3 only (20, 30, 40). See execution_table step 2.
Does modifying the slice change the original array?
Yes, if the slice is mutable, changes to the slice affect the original array because the slice is just a view into the array's memory. In this example, the slice is immutable, so no changes occur. See concept_flow and variable_tracker.
What happens if we try to access slice[3] in this example?
It would cause a runtime panic because the slice length is 3 (indices 0 to 2). Accessing index 3 is out of bounds. This is prevented by Rust's safety checks.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what elements does the slice contain?
A[20, 30, 40]
B[10, 20, 30]
C[30, 40, 50]
D[10, 20, 30, 40]
💡 Hint
Check the Slice Content column at step 2 in the execution_table.
At which step is the output '20' produced?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Output column in the execution_table.
If we changed the slice to &arr[0..3], what would slice[0] be?
A30
B20
C10
D40
💡 Hint
Recall that slice[0] is the first element of the slice range; see concept_flow and execution_table.
Concept Snapshot
Array slices in Rust let you view a part of an array without copying.
Syntax: &array[start..end] where start is inclusive, end exclusive.
Slices are references; they do not own data.
You can read or modify elements via mutable slices.
Slices prevent out-of-bounds access at runtime.
Slices keep the original array unchanged unless mutated through them.
Full Transcript
This visual trace shows how Rust array slices work. We start with an array of five numbers. Then we create a slice that views elements from index 1 up to but not including 4. This slice contains three elements: 20, 30, and 40. When we print the first element of the slice, it outputs 20. The slice is just a view into the original array, so the array remains unchanged throughout. Rust slices use ranges where the start index is included and the end index is excluded. Trying to access outside the slice bounds causes a runtime error. Mutable slices can change the original array, but this example uses an immutable slice. This helps safely work with parts of arrays without copying data.

Practice

(1/5)
1. What is an array slice in Rust?
easy
A. A new array created by copying elements
B. A macro to print arrays
C. A function that modifies an array
D. A reference to a part of an array without copying data

Solution

  1. Step 1: Understand what a slice is

    A slice is a reference to a contiguous part of an array, not a new array.
  2. Step 2: Compare options

    Only A reference to a part of an array without copying data correctly describes a slice as a reference without copying.
  3. Final Answer:

    A reference to a part of an array without copying data -> Option D
  4. Quick Check:

    Slice = reference to part of array [OK]
Hint: Remember: slices borrow, they don't copy [OK]
Common Mistakes:
  • Thinking slices create new arrays
  • Confusing slices with functions
  • Assuming slices own data
2. Which of the following is the correct syntax to create a slice from an array arr in Rust?
easy
A. let slice = &arr[1..4];
B. let slice = arr.slice(1, 4);
C. let slice = arr[1:4];
D. let slice = &arr(1..4);

Solution

  1. Step 1: Recall Rust slice syntax

    Rust uses &array[start..end] to create a slice referencing elements from start up to but not including end.
  2. Step 2: Check each option

    let slice = &arr[1..4]; uses correct Rust syntax. Options A, B, and C use invalid syntax.
  3. Final Answer:

    let slice = &arr[1..4]; -> Option A
  4. Quick Check:

    Slice syntax = &arr[start..end] [OK]
Hint: Use &array[start..end] for slices [OK]
Common Mistakes:
  • Using parentheses instead of brackets
  • Using colons instead of two dots
  • Calling slice as a method
3. What is the output of this Rust code?
let arr = [10, 20, 30, 40, 50];
let slice = &arr[1..4];
println!("{:?}", slice);
medium
A. [30, 40, 50]
B. [10, 20, 30]
C. [20, 30, 40]
D. [1, 4]

Solution

  1. Step 1: Understand the slice range

    The slice &arr[1..4] includes elements at indices 1, 2, and 3 (20, 30, 40).
  2. Step 2: Check the printed output

    Printing the slice shows the elements in the slice as an array-like list.
  3. Final Answer:

    [20, 30, 40] -> Option C
  4. Quick Check:

    Slice arr[1..4] = [20, 30, 40] [OK]
Hint: Remember slice excludes the end index element [OK]
Common Mistakes:
  • Including the element at index 4
  • Starting slice at index 0
  • Printing the slice as indices
4. Identify the error in this Rust code snippet:
let arr = [1, 2, 3, 4, 5];
let slice = &arr[2..6];
println!("{:?}", slice);
medium
A. The slice range goes out of bounds
B. The slice syntax is incorrect
C. The array is not mutable
D. The println! macro is used incorrectly

Solution

  1. Step 1: Check the slice range

    The array has length 5, valid indices are 0 to 4. The slice uses 2..6, which tries to access index 5 (out of bounds).
  2. Step 2: Identify the error type

    Rust will panic at runtime due to out-of-bounds slicing.
  3. Final Answer:

    The slice range goes out of bounds -> Option A
  4. Quick Check:

    Slice range must be within array length [OK]
Hint: Slice end index must be ≤ array length [OK]
Common Mistakes:
  • Using an end index beyond array length
  • Confusing syntax errors with runtime errors
  • Assuming arrays auto-expand
5. Given a function that takes a slice of integers and returns the sum of its elements, which of the following implementations is correct?
fn sum_slice(slice: &[i32]) -> i32 {
    // fill in
}
hard
A. slice.to_vec().sum()
B. slice.iter().sum()
C. slice[0] + slice[1]
D. slice.sum()

Solution

  1. Step 1: Understand slice methods

    A slice has an iterator via .iter(), and iterators have a .sum() method to add elements.
  2. Step 2: Evaluate each option

    slice.iter().sum() correctly uses slice.iter().sum(). slice.to_vec().sum() is valid because Vec<i32> has sum() method. slice[0] + slice[1] only sums two elements, not all. slice.sum() is invalid because slices don't have sum() directly.
  3. Final Answer:

    slice.iter().sum() -> Option B
  4. Quick Check:

    Use iter().sum() to sum slice elements [OK]
Hint: Use slice.iter().sum() to sum elements [OK]
Common Mistakes:
  • Trying to call sum() directly on slice
  • Summing only first two elements
  • Calling sum() on Vec<i32>
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