From the course: Rust Essential Training

Unlock this course with a free trial

Join today to access over 25,600 courses taught by industry experts.

Solution: Higher or lower

Solution: Higher or lower - Rust Tutorial

From the course: Rust Essential Training

Solution: Higher or lower

(upbeat music) - [Instructor] My solution for the higher or lower guessing game challenge relied on the rand crate to generate the random number. So the first thing I did after creating a new project was update Cargo.toml to include the rand crate as a dependency. Then, in the main source file, I imported the rand crate's prelude with a use statement. I also imported the standard libraries IO module because I'll be using that for the player to enter their guesses through standard input. At the beginning of the main function, at line five, I use the rand library to generate a random number between 1 and 100. Then, after lines seven and eight print messages prompting the player to guess the number, the program enters a loop. I chose to use the loop construct here rather than a for-loop because I don't know how many times the player will need to guess to get the number right. If they're a bad guesser, they could be at it…

Contents