A slice is a reference to a contiguous piece of a collection — not the whole thing, not a copy, just a borrowed window into part of it.
String slices
&str is a string slice: a borrowed view into string data. &s[0..5] borrows the first five bytes of a String. A string literal like "hello" is itself a &str — a slice pointing into your compiled binary. This is the deeper reason functions should take &str: it accepts both literals and borrowed Strings.
Array and vector slices
The same idea works on any sequence. &v[1..3] borrows a window into a vector — a slice of type &[T]. The slice knows its start and length; it doesn't own the elements, so it can't outlive the collection it points into. The borrow checker guarantees that.
&str and &[T], not &String and &Vec. The slice forms are more general: a &str parameter accepts literals, full Strings, and substrings alike. Idiomatic Rust signatures borrow slices, not owned-collection references.Why slices are safe here and unsafe in C
In C, a pointer-plus-length into an array is a bug waiting to happen — free the array and the pointer dangles. In Rust, a slice borrows the collection, so the borrow checker won't let the collection be dropped or mutated while the slice is alive. Same performance, none of the danger.