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
- [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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.