From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Slices

Slices - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Slices

- [Narrator] When borrowing data types that hold a sequence of elements like an array or a string, if we only want to reference a subset of those elements rather than the entire collection, we can create what's called a slice which references a contiguous sequence of elements within a collection without taking ownership of them. One of the most commonly used types of slices is the string slice whose data type is written as ampersand lowercase STR. We've actually been using that string slice data type throughout this entire course every time we write a string literal. The data for the string literal is hard-coded into the executable binary and the program uses a string slice to access it. To demonstrate a string slice, let's create a reference to the string online too with a message, greetings from earth that slices out just the last word, earth. We'll declare a new variable named last word, and then use the borrow operator…

Contents