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.
Solution: Trim spaces - Rust Tutorial
From the course: Rust Essential Training
Solution: Trim spaces
(upbeat music) - My solution to the challenge begins with the trim space function at line 25. The functions input type is a string slice which enables it to accept both string references and slices. And the output type is also a slice. Within the function I use the for loop online 28 to search through the input string starting at the beginning to locate the first non-space character. I'm using the care's method on the string S to get an iterator over the characters in the string and then using the enumerate method to get the iteration count along with the character value. The if expression online 29 checks to see if the character is anything other than space and if so I saved the current index to the start variable which I'll use to create the final slice. I don't need to search any further after that so I break out of the loop. I can iterate through the string and index it in terms of characters here instead of fights…
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.