Bird
Raised Fist0
Rustprogramming~10 mins

Slice syntax in Rust - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Slice syntax
Start with array or vector
↓
Specify start index
→Specify end index
↓
Create slice from start to end-1
↓
Use slice for read or iteration
↓
End
You start with an array or vector, pick a start and end index, then create a slice that includes elements from start up to but not including end.
Execution Sample
Rust
let arr = [10, 20, 30, 40, 50];
let slice = &arr[1..4];
println!("{:?}", slice);
This code creates a slice from index 1 to 3 of the array and prints it.
Execution Table
StepActionIndex RangeSlice ContentOutput
1Define array-[10, 20, 30, 40, 50]-
2Create slice from arr[1..4]1 to 3[20, 30, 40]-
3Print slice--[20, 30, 40]
4End---
💡 Slice created from index 1 up to but not including 4; execution ends after printing.
Variable Tracker
VariableStartAfter 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]
sliceN/A[20, 30, 40][20, 30, 40][20, 30, 40]
Key Moments - 3 Insights
Why does the slice include elements up to index 3 but not index 4?
Because Rust slice syntax uses start..end where end is exclusive, so elements are taken from start up to end-1. See execution_table step 2.
What happens if the start index is equal to the end index?
The slice will be empty because it includes elements from start up to but not including end. This means no elements are selected.
Can you modify the original array through a slice?
Only if the slice is mutable. Here the slice is immutable (&arr[1..4]), so you cannot change the array elements through it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of the slice at step 2?
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 slice printed to output?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table to find when the slice is printed.
If the slice was created with &arr[2..5], what would be the slice content?
A[20, 30, 40]
B[30, 40, 50]
C[10, 20, 30]
D[40, 50]
💡 Hint
Remember slice includes elements from start index up to but not including end index.
Concept Snapshot
Slice syntax in Rust: &array[start..end]
Includes elements from start index up to but not including end index.
Start and end must be within array bounds.
Slices are views, no data copied.
Use slices for safe, efficient access to parts of arrays or vectors.
Full Transcript
This visual execution shows how Rust slice syntax works. We start with an array of five numbers. Then we create a slice from index 1 to 4, which includes elements at indices 1, 2, and 3. The slice is printed showing [20, 30, 40]. The slice does not include the element at index 4 because the end index is exclusive. Variables arr and slice are tracked through the steps. Key moments clarify why the end index is exclusive, what happens if start equals end, and that slices are immutable unless declared mutable. The quiz tests understanding of slice content, printing step, and how changing indices affects the slice. The snapshot summarizes the slice syntax and behavior in Rust.

Practice

(1/5)
1. What does the slice syntax &array[2..5] mean in Rust?
easy
A. It creates a view of elements from index 2 up to but not including 5.
B. It copies elements from index 2 to 5 inclusive into a new array.
C. It creates a slice starting at index 5 and ending at index 2.
D. It returns the element at index 2 only.

Solution

  1. Step 1: Understand slice range syntax

    The syntax start..end means from start index up to but not including end index.
  2. Step 2: Apply to given slice

    So &array[2..5] means elements at indices 2, 3, and 4 are included in the slice.
  3. Final Answer:

    It creates a view of elements from index 2 up to but not including 5. -> Option A
  4. Quick Check:

    Slice range excludes end index = A [OK]
Hint: Remember slice excludes the end index, like half-open intervals [OK]
Common Mistakes:
  • Thinking the slice copies elements instead of borrowing
  • Including the end index element in the slice
  • Confusing slice with single element access
2. Which of the following is the correct way to get a slice of the first 4 elements of an array named nums?
easy
A. &nums[1..4]
B. &nums[0..4]
C. &nums[0..5]
D. &nums[4..0]

Solution

  1. Step 1: Identify slice start and end for first 4 elements

    The first 4 elements are at indices 0, 1, 2, and 3, so slice should start at 0 and end at 4 (exclusive).
  2. Step 2: Check options for correct syntax

    &nums[0..4] uses &nums[0..4], which correctly slices elements 0 to 3.
  3. Final Answer:

    &nums[0..4] -> Option B
  4. Quick Check:

    Slice first 4 elements = &nums[0..4] [OK]
Hint: Start at 0, end at count for first N elements [OK]
Common Mistakes:
  • Starting slice at 1 instead of 0
  • Including one extra element by using end index too high
  • Using reversed range which is invalid
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. [20, 30, 40, 50]
B. [10, 20, 30, 40]
C. [20, 30, 40]
D. [30, 40, 50]

Solution

  1. Step 1: Identify slice indices and elements

    The slice &arr[1..4] includes elements at indices 1, 2, and 3, which are 20, 30, and 40.
  2. Step 2: Understand print output

    Printing the slice with {:?} shows the elements in array-like format.
  3. Final Answer:

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

    Slice arr[1..4] = [20, 30, 40] [OK]
Hint: Slice excludes end index, so count elements carefully [OK]
Common Mistakes:
  • Including element at index 4 mistakenly
  • Printing the whole array instead of slice
  • Confusing slice with copying elements
4. What is wrong with this Rust code?
let arr = [1, 2, 3, 4];
let slice = &arr[3..2];
medium
A. The slice start index is greater than the end index, causing a panic.
B. The slice syntax is correct and will return an empty slice.
C. The code will compile but slice will contain elements 3 and 2.
D. The slice must use parentheses instead of brackets.

Solution

  1. Step 1: Analyze slice indices order

    The slice &arr[3..2] has start index 3 and end index 2, which is invalid because start must be less than or equal to end.
  2. Step 2: Understand Rust slice behavior

    Rust panics at runtime if slice start is greater than end; it does not return empty slice or reorder indices.
  3. Final Answer:

    The slice start index is greater than the end index, causing a panic. -> Option A
  4. Quick Check:

    Slice start > end causes panic [OK]
Hint: Slice start must be ≤ end; else panic occurs [OK]
Common Mistakes:
  • Assuming reversed slice returns empty slice
  • Thinking slice silently swaps indices
  • Using parentheses instead of brackets for slices
5. Given a string let text = "hello world";, which slice expression extracts the word "world" correctly?
hard
A. &text[..5]
B. &text[5..10]
C. &text[6..10]
D. &text[6..]

Solution

  1. Step 1: Identify indices of "world" in the string

    "hello world" has "world" starting at index 6 and goes to the end (index 10 inclusive).
  2. Step 2: Check slice options

    &text[6..] uses &text[6..] which means from index 6 to the end, correctly extracting "world".
  3. Final Answer:

    &text[6..] -> Option D
  4. Quick Check:

    Slice from start index to end = &text[6..] [OK]
Hint: Use start.. to slice from start index to string end [OK]
Common Mistakes:
  • Using wrong end index excluding last character
  • Confusing byte indices with character indices (not in this example)
  • Using slice that extracts wrong substring
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