This is a classical beginner programming problem: a guessing game. The program would first need to generate a random integer between 1 and 100. It then prompts the player to enter a guess. After entering a guess, it indicates whether the guess is too low or too high. If the guess is correct, the game prints congratulations and exits.
- [Instructor] Let's put to use whatever we have learned in this course by implementing a classic beginner programming problem: The Guessing Game. Here's how it works. The program will generate a random integer between one and 100. It'll then prompt the player to enter a guess. After entering a guess, it'll indicate whether the guess is too low or too high. If the guess is correct, the game will print congrats and exit. Let's first set up a new project and call it "guessing game." As you may recall, we use cargo new followed by the name of the project, and then bin.
Recall, the bin flag tells cargo to make a binary project, and generates our main.rs file. Let's open that up. Let's remove the standard code here. The first part of our program will ask for user input, process that input, and check that the input is in the expected form. To obtain user input and then print a result as output, we need to bring the io library into scope. io stands for input-output.
Now, this is how we use that. There's a key statement, and use std followed by double colon and io. By default, rs brings only a few types into the scope of every program. If a type you want to use isn't in the prelude, you have to bring that type into the scope explicitly, with a use statement, and that's what we have done here. Now let's go to the main function, and just say print ln, and "please input your guess." Now we need to create a mutable variable to capture our guess.
It needs to be mutable because we'll do some computation with it later on. Let mut, guess, equals, string, and new. And since the user is allowed to type anything, let's make it as a string type. We've used the new operator here. The new function creates a new empty string. You'll find new function on many types, because it's a common name for a function that makes a new value of some kind.
Now that we've got the variable, we need to process the input and store it in this variable. Let's see how we can do that. Use the io library, stdin, and then there's a read line function in this type. The parameter we pass here would be the parameter in which we want to store the input value. And this is of mutable type.
Now this function call may be successful, or it may result to a failure. So we use the expect method to indicate whether if there is a failure, and we can have our own custom message printed on the screen in case of an error. So let's say "failed to read line." And let's end this with a semicolon. So if we see the code now, we have a variable guess, and we're trying to read it from our input and we're trying to store it in the variable guess.
So let's go ahead and print that out. So print ln, "you guessed," and the guess. So recall that we're using the placeholder to print any value. So let's save this and go to the command prompt to run our code. Recall that we use the cargo run command to run the code.
Ugh, before that let's cd into the guessing game directory and then do cargo run. So on line one, when we use the use statement, just remember to end it with the semicolon. Let's clear the console and then run it again. Third time's the charm, I hope. Yeah. As you can see, the program is asking for our input. Let's enter a value. And then, the compiler says that you guessed five.
- Installing Rust
- Data types
- Adjusting memory allocation
- Setting up race conditions
- Working with functions
- Exploring references and borrowing
- Using structs
- Method syntax
- Demonstrating recoverable errors with Result
- Working with enums
- Getting user input