1. What is a Slice?
A slice is a reference to a continuous portion of a collection.
Instead of accessing the entire collection, we can access only the part we need.
For example, suppose we have an array:
We can create a slice containing only:
Example
Output:
Here:
numbersis the original array.&numbers[1..4]creates a reference to part of the array.slicerefers to the selected elements.
The original array is not copied.
2. Understanding Slice Syntax
The general syntax is:
The starting index is included, but the ending index is excluded.
Example:
Index positions:
Index | Value |
|---|---|
0 | 10 |
1 | 20 |
2 | 30 |
3 | 40 |
4 | 50 |
The range 1..4 selects indexes 1, 2, and 3.
Therefore, the result is:
3. Different Ways to Create Slices
Rust provides different ways to specify slice ranges.
Consider this array:
A. Select a Specific Range
Output:
B. Start from the Beginning
You can omit the starting index.
Output:
C. Continue Until the End
You can omit the ending index.
Output:
D. Select the Entire Array
Output:
Quick Reference
Syntax | Meaning |
|---|---|
| Index 1 up to, but not including, 3 |
| Beginning up to index 3 |
| Index 2 to the end |
| Entire collection |
4. What is the Slice Data Type?
For an array of integers, the slice type is &[i32].
Example:
Output:
Here:
&means a reference.[i32]means a slice of integers.&[i32]means a borrowed reference to an integer slice.
Unlike an array, a slice does not contain its own fixed length in its type. Its length is available at runtime.
5. Finding the Length of a Slice
Use the .len() method to find the number of elements in a slice.
Example:
Output:
6. Accessing Slice Elements
We can access individual slice elements using indexing.
Example:
Output:
Remember: The index of a slice starts from zero, regardless of its position in the original array.
7. Mutable Slices
A mutable slice allows us to modify elements of the original collection.
Use &mut to create a mutable slice.
Example:
Output:
Here:
- The original array is declared using
mut. &mut numbers[1..4]creates a mutable slice.- Changes made through the slice affect the original array.
8. Using Slices with Functions
Slices are especially useful when passing collections to functions.
Instead of requiring a function to accept an array of a specific length, we can accept a slice.
Example: Display Array Elements
Output:
Here:
&[i32]means the function accepts a borrowed integer slice.&numberspasses a reference to the entire array.- The function can read the values without taking ownership.
Important Advantage
The same function can accept arrays of different lengths.
Output:
This makes functions more flexible.
9. What is a String Slice?
A string slice is a reference to a portion of a string.
Its type is &str.
Example:
Output:
Here:
nameis an ownedString.&name[0..5]creates a string slice.slicerefers to the first five bytes of the string.
10. Different Ways to Create String Slices
Consider this string:
A. Specific Range
Output:
B. From the Beginning
Output:
C. Until the End
Output:
D. Entire String
Output:
11. Important: UTF-8 String Slicing
Rust strings use UTF-8 encoding.
This means that not every character occupies exactly one byte.
For example:
Output:
This works because the selected byte positions are valid UTF-8 character boundaries.
However, slicing at an invalid character boundary can cause a runtime panic.
For example, this is unsafe:
This can panic because byte index 1 is not a valid character boundary in this string.
Important: Use .chars() when you want to iterate over Unicode scalar values rather than working with byte positions.
12. String Slices vs String
Feature | String | String Slice (&str) |
|---|---|---|
Ownership | Owns its text | Borrows text |
Growable | Yes | No |
Can modify text through it? | Yes, if mutable | No |
Example |
|
|
Common use | Storing and modifying text | Reading or passing text |
13. Returning a Slice from a Function
A function can return a string slice that refers to data owned by its caller.
Example:
Output:
Here:
find(' ')searches for the first space.Some(index)contains the position of the first space.&text[..index]returns the text before that space.- If no space exists, the function returns the complete string slice.
The returned slice borrows from the input string. It does not own a separate string.
14. Difference Between Array, Slice and String Slice
Feature | Array | Array Slice | String Slice |
|---|---|---|---|
Type example |
|
|
|
Stores | Fixed collection | Reference to part or all of an array | Reference to text |
Element types | Same type | Same type | UTF-8 text |
Size | Fixed | Length known at runtime | Length in bytes known at runtime |
Example |
|
|
|
15. Important Notes
- A slice is a reference to a continuous portion of a collection.
- Use
&to create an immutable slice. - Use
&mutto create a mutable slice. - Slice ranges include the starting index but exclude the ending index.
- Array slices use the type
&[T]. - String slices use the type
&str. - Slices do not take ownership of the original collection.
- Array and string indexing starts from zero.
- String slices must use valid UTF-8 character boundaries.
- Use slices in function parameters when you want to accept collections of different lengths.
16. Quick Reference
Task | Syntax |
|---|---|
Array slice |
|
Slice from beginning |
|
Slice to end |
|
Entire array slice |
|
Mutable array slice |
|
Slice length |
|
Array slice type |
|
String slice |
|
String slice type |
|
Iterate over slice |
|