An array slice is a reference to a contiguous sequence of elements in an array. It lets you view or work with a part of the array without copying it.
Click to reveal answer
beginner
How do you create a slice from an array in Rust?
You use the syntax &array[start..end] where start is inclusive and end is exclusive. For example, &arr[1..4] creates a slice from index 1 up to but not including 4.
Click to reveal answer
intermediate
What is the difference between &array and &array[..] in Rust?
&array is a reference to the whole array, while &array[..] is a slice that covers the entire array. The slice is a view and can be passed to functions expecting slices.
Click to reveal answer
intermediate
Can you modify the elements of an array through a slice in Rust?
Yes, but only if the slice is mutable. You create a mutable slice with &mut array[start..end]. This lets you change the elements inside that slice.
Click to reveal answer
beginner
Why are slices useful in Rust?
Slices let you work with parts of arrays safely without copying data. They help write efficient code and allow functions to accept any size of data without needing ownership.
Click to reveal answer
How do you create a slice of the first three elements of an array named arr?
A&arr[0..3]
B&arr[1..3]
Carr[0..3]
Darr.slice(0,3)
✗ Incorrect
The correct syntax is &arr[0..3] to create a slice from index 0 up to but not including 3.
What type does a slice have in Rust?
A[T; N]
BVec<T>
C&[T]
DT
✗ Incorrect
A slice has the type &[T], which is a reference to a sequence of elements of type T.
Can you change elements through an immutable slice?
AOnly if the slice is created with &mut
BYes, always
CYes, but only if the array is mutable
DNo, only through mutable slices
✗ Incorrect
You cannot modify elements through an immutable slice. You need a mutable slice created with &mut to change elements.
What happens if you try to create a slice with an out-of-bounds range?
ARust panics at runtime
BIt returns an empty slice
CIt compiles but returns garbage data
DIt silently ignores the invalid part
✗ Incorrect
Rust checks slice bounds at runtime and will panic if the range is out of bounds.
Which of these is a valid way to get a slice of the entire array arr?
A&arr[1..]
B&arr[..]
C&arr[0]
Darr.slice()
✗ Incorrect
&arr[..] creates a slice covering the whole array.
Explain what an array slice is in Rust and how it differs from an array reference.
Think about how slices let you see part of an array without owning it.
You got /4 concepts.
Describe how to create and use a mutable slice to change elements in an array.
Remember you need &mut to change data through a slice.
You got /4 concepts.
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
Step 1: Understand what a slice is
A slice is a reference to a contiguous part of an array, not a new array.
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.
Final Answer:
A reference to a part of an array without copying data -> Option D
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
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.
Step 2: Check each option
let slice = &arr[1..4]; uses correct Rust syntax. Options A, B, and C use invalid syntax.
Final Answer:
let slice = &arr[1..4]; -> Option A
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
Step 1: Understand the slice range
The slice &arr[1..4] includes elements at indices 1, 2, and 3 (20, 30, 40).
Step 2: Check the printed output
Printing the slice shows the elements in the slice as an array-like list.
Final Answer:
[20, 30, 40] -> Option C
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
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).
Step 2: Identify the error type
Rust will panic at runtime due to out-of-bounds slicing.
Final Answer:
The slice range goes out of bounds -> Option A
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
Step 1: Understand slice methods
A slice has an iterator via .iter(), and iterators have a .sum() method to add elements.
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.
Final Answer:
slice.iter().sum() -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.