Member-only story
Slices
Yesterday we checked out Loops in-depth and other niceties around them, if you missed it, I’d recommend checking it out below.
In this post, let’s discuss Slices, it’s day 19 here we go!
A slice in Rust is like a window into a portion of a sequence, such as an array, vector, or string. It’s a reference to a contiguous chunk of data, letting you work with just that part without taking ownership or copying anything.
Think of it as borrowing a few pages from a book instead of buying the whole book or photocopying it. This makes slices memory-efficient and fast, which is critical in performance-sensitive applications like game development or data processing.
Slices come in two main flavors: array/vector slices (&[T]) and string slices (&str). Both are references, meaning they don’t own the data they point to, and they’re defined by a starting point and a length.
For example, if you have an array of numbers, a slice might let you focus on just the first three elements without touching the rest.

