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: 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…
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.