From the course: Rust Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Solution: Handle errors - Rust Tutorial
From the course: Rust Essential Training
Solution: Handle errors
(upbeat music) - [Instructor] Here's my solution to the challenge to add error handling to the higher or lower game. Since the read line and parse methods are the two parts of this program that could potentially produce an error, and calling parse is dependent on the read line method having been successful, so there's something in the buffer to parse, I implemented my error handling by putting them in a pair of nested match statements. If the call to read line on line 11 is successful, then the parse method will get called on line 12 to attempt to parse the contents of the buffer into a u32 value. If that is then successful, the match arm on line 13 returns that value, which gets assigned to the guess variable. If the result from either of those two method calls is an error, then one of the match arms on line 14 or 19 will execute. Those code blocks print a message describing what failed, prompt the user to guess again,…
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.