Back to course

The Slice Type

Rust for Systems & Web3 Security

Slices: Views into Data

Slices let you reference a contiguous sequence of elements in a collection rather than the whole collection. They are a kind of reference, so they don't have ownership.

rust let s = String::from("hello world"); let hello = &s[0..5]; let world = &s[6..11];

String Slices

In Rust, &str is a string slice. It's more efficient than String because it's just a pointer and a length. When designing APIs, preferring &str makes them more flexible.