Bird
Raised Fist0
Rustprogramming~10 mins

Why slices are used in Rust - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why slices are used
Have a collection (array or vector)
↓
Create a slice: reference part of collection
↓
Use slice to access or process subset
↓
Avoid copying data, save memory and time
↓
Slice ends when done, original data stays intact
Slices let you look at part of a collection without copying it, saving memory and making code faster.
Execution Sample
Rust
let arr = [10, 20, 30, 40, 50];
let slice = &arr[1..4];
println!("Slice: {:?}", slice);
This code creates a slice of the array from index 1 to 3 and prints it.
Execution Table
StepActionValue/ResultExplanation
1Create array arr[10, 20, 30, 40, 50]Array with 5 elements created
2Create slice from arr[1..4][20, 30, 40]Slice references elements at index 1,2,3 without copying
3Print sliceSlice: [20, 30, 40]Output shows the slice content
4End-Program ends, original array unchanged
💡 Program ends after printing the slice; slice used to access part of array without copying
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
arrundefined[10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50][10, 20, 30, 40, 50]
sliceundefinedundefined[20, 30, 40][20, 30, 40][20, 30, 40]
Key Moments - 2 Insights
Why does the slice not copy the data from the array?
Because slices are references to parts of the original data, they only point to it without making a new copy, as shown in step 2 of the execution_table.
What happens if we change the original array after creating the slice?
The slice will see the updated data because it references the original array, not a copy. This is why slices are efficient and safe to use.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of slice after step 2?
A[10, 20, 30]
B[20, 30, 40]
C[30, 40, 50]
D[10, 20, 30, 40, 50]
💡 Hint
Check the 'Value/Result' column for step 2 in the execution_table.
At which step is the slice printed to the screen?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the 'Print slice' action in the execution_table.
If we changed the slice to &arr[0..3], what would slice contain after step 2?
A[30, 40, 50]
B[20, 30, 40]
C[10, 20, 30]
D[40, 50]
💡 Hint
Slices include elements from the start index up to but not including the end index.
Concept Snapshot
Slices in Rust:
- Are references to parts of arrays or vectors
- Do not copy data, saving memory
- Syntax: &collection[start..end]
- Include elements from start up to but not including end
- Useful for efficient, safe access to data subsets
Full Transcript
This visual trace shows how slices in Rust work. First, an array is created with five numbers. Then, a slice is made by referencing part of the array from index 1 to 3. This slice does not copy the data but points to the original array's elements. When printed, the slice shows the selected elements. The program ends with the original array unchanged. Slices help save memory and time by avoiding data copying while allowing safe access to parts of collections.

Practice

(1/5)
1. Why are slices used in Rust?
let arr = [1, 2, 3, 4]; let slice = &arr[1..3];
easy
A. To access a part of a collection without copying the data
B. To create a new independent array from the original
C. To permanently delete elements from the array
D. To convert an array into a string

Solution

  1. Step 1: Understand what a slice does

    A slice is a reference to a part of a collection, like a section of an array, without copying the data.
  2. Step 2: Analyze the example code

    The code &arr[1..3] creates a slice referencing elements at index 1 and 2, not a new array or deletion.
  3. Final Answer:

    To access a part of a collection without copying the data -> Option A
  4. Quick Check:

    Slices = references to parts of collections [OK]
Hint: Slices reference parts without copying data [OK]
Common Mistakes:
  • Thinking slices copy data
  • Confusing slices with new arrays
  • Assuming slices delete elements
2. Which of the following is the correct syntax to create a slice of the first three elements of an array arr in Rust?
easy
A. let slice = arr[0..3];
B. let slice = arr[0, 3];
C. let slice = arr.slice(0, 3);
D. let slice = &arr[0..3];

Solution

  1. Step 1: Recall slice syntax in Rust

    Rust slices are references, so you must use & before the range to create a slice.
  2. Step 2: Check each option

    let slice = &arr[0..3]; uses &arr[0..3], which is the correct syntax. let slice = arr[0..3]; misses the reference, B uses invalid syntax, and C uses a non-existent method.
  3. Final Answer:

    let slice = &arr[0..3]; -> Option D
  4. Quick Check:

    Slice syntax = &array[start..end] [OK]
Hint: Slices need & before range to reference data [OK]
Common Mistakes:
  • Omitting & before the slice range
  • Using invalid methods like slice()
  • Using comma instead of range syntax
3. What will be the output of this Rust code?
let arr = [10, 20, 30, 40, 50];
let slice = &arr[2..4];
println!("{:?}", slice);
medium
A. [30, 40]
B. [20, 30]
C. [30, 40, 50]
D. [10, 20]

Solution

  1. Step 1: Understand the slice range

    The slice &arr[2..4] includes elements at index 2 and 3 (end index 4 is exclusive).
  2. Step 2: Identify elements at those indices

    Index 2 is 30, index 3 is 40, so the slice contains [30, 40].
  3. Final Answer:

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

    Slice range excludes end index [OK]
Hint: Slice end index is exclusive, so stop before it [OK]
Common Mistakes:
  • Including the end index element
  • Confusing indices with values
  • Printing the whole array instead of slice
4. Find the error in this Rust code that tries to create a slice:
let arr = [1, 2, 3, 4];
let slice = arr[1..5];
medium
A. Array declaration syntax is wrong
B. Slice range is out of bounds and missing reference operator
C. Slice syntax is correct, no error
D. Slice must use parentheses instead of brackets

Solution

  1. Step 1: Check the slice range validity

    The array has length 4, so index 5 is out of bounds causing an error.
  2. Step 2: Check slice syntax

    The code misses the & reference operator needed to create a slice.
  3. Final Answer:

    Slice range is out of bounds and missing reference operator -> Option B
  4. Quick Check:

    Slice must be &array[start..end] within bounds [OK]
Hint: Slice must be referenced and within array bounds [OK]
Common Mistakes:
  • Ignoring out of bounds index
  • Forgetting & before slice
  • Thinking brackets are wrong syntax
5. You want to write a function that takes a string slice and prints its first 3 characters safely. Which function signature and usage is correct in Rust?
hard
A. fn print_first_three(s: String) { println!("{}", &s[0..3]); }
B. fn print_first_three(s: &String) { println!("{}", s[0..3]); }
C. fn print_first_three(s: &str) { println!("{}", &s[0..3]); }
D. fn print_first_three(s: &str) { println!("{}", s[0..3]); }

Solution

  1. Step 1: Understand string slices and indexing

    String slices &str are references to string data and support slicing with range syntax and & to get a slice.
  2. Step 2: Analyze each function signature and usage

    fn print_first_three(s: &str) { println!("{}", &s[0..3]); } correctly takes &str and slices with &s[0..3]. fn print_first_three(s: String) { println!("{}", &s[0..3]); } takes ownership (String) but slices correctly. fn print_first_three(s: &String) { println!("{}", s[0..3]); } misses & before slice. fn print_first_three(s: &str) { println!("{}", s[0..3]); } slices without &, causing error.
  3. Final Answer:

    fn print_first_three(s: &str) { println!("{}", &s[0..3]); } -> Option C
  4. Quick Check:

    Use &str and &s[start..end] for safe string slices [OK]
Hint: Use &str and &s[start..end] to slice strings safely [OK]
Common Mistakes:
  • Using String instead of &str for slicing
  • Omitting & before slice range
  • Trying to slice without reference
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