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 as function parameters - Rust Tutorial
From the course: Rust Essential Training
Slices as function parameters
- [Instructor] Being able to reference only part of a collection is incredibly useful and you'll commonly see slices used as function inputs and outputs, especially on functions that work with strings. For example, the get_first_word function shown here on line seven takes a borrowed reference to an entire string as input and then returns a slice containing just the first word of that input string all without taking ownership of the string. It does that by converting the input string reference into a slice of bytes representing its contents and then iterates through that slice of bytes one at a time, checking for the byte sequence that represents a space character with the if expression on line 11. If it finds a space, that means we've reached the end of a word and it returns a slice of the input string ranging from its beginning up to the index where we found the first space. If the for-loop searches the entire string…
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.