Skip to main content
In this lesson, we’ll explore slices in Rust—a powerful feature that allows you to reference a contiguous sequence of elements within a collection without creating a copy. Slices are commonly used with arrays, vectors, and strings, offering both efficiency and safety.

What Is a Slice?

A slice is a reference to a segment of a collection. Instead of copying data, a slice provides a window into an array, vector, or string. This is particularly useful when you want to work with a subset of data without the overhead of duplication. The syntax for creating a slice uses a range within square brackets:
  • The start index is inclusive, which means it marks the beginning of the slice.
  • The end index is exclusive, indicating where the slice ends.
Remember that the range syntax helps prevent out-of-bounds errors by enforcing that the slice only covers valid indices of the collection.

Array Slices in Rust

Consider an array with the elements [1, 2, 3, 4, 5]. You can create a slice that references specific elements—in this case, from index 1 up to, but not including, index 4. The resulting slice contains the values 2, 3, and 4.
The diagram below illustrates the array and its indices:
When executed, the code produces the output: [2, 3, 4].

String Slices in Rust

Slices work similarly with strings, allowing you to reference a portion of a string without copying it. For example, consider the string "hello, world":
In this example:
  • The slice from index 0 to 5 returns "hello".
  • The slice from index 7 to 12 returns "world".
When compiled and executed, the code outputs: hello, world.

Mutable Slices in Rust

Mutable slices allow you to modify the referenced elements directly. In the following example, we start with a mutable array and create a mutable slice. Then, we update an element within the slice:
Even though the slice starts at index 1, modifying slice[0] corresponds to changing the second element of the original array.
Always remember that modifying slices can affect the original collection. Ensure that your slice does not outlive the data it references to avoid unexpected behavior.

Advantages of Using Slices

Slices offer several benefits: By using slices, you can write more efficient and robust Rust code. Explore more about Rust slices to enhance your programming efficiency and ensure better memory management.

Watch Video