]> git.proxmox.com Git - rustc.git/blob - src/doc/book/src/ch02-00-guessing-game-tutorial.md
New upstream version 1.66.0+dfsg1
[rustc.git] / src / doc / book / src / ch02-00-guessing-game-tutorial.md
1 # Programming a Guessing Game
2
3 Let’s jump into Rust by working through a hands-on project together! This
4 chapter introduces you to a few common Rust concepts by showing you how to use
5 them in a real program. You’ll learn about `let`, `match`, methods, associated
6 functions, external crates, and more! In the following chapters, we’ll explore
7 these ideas in more detail. In this chapter, you’ll just practice the
8 fundamentals.
9
10 We’ll implement a classic beginner programming problem: a guessing game. Here’s
11 how it works: the program will generate a random integer between 1 and 100. It
12 will then prompt the player to enter a guess. After a guess is entered, the
13 program will indicate whether the guess is too low or too high. If the guess is
14 correct, the game will print a congratulatory message and exit.
15
16 ## Setting Up a New Project
17
18 To set up a new project, go to the *projects* directory that you created in
19 Chapter 1 and make a new project using Cargo, like so:
20
21 ```console
22 $ cargo new guessing_game
23 $ cd guessing_game
24 ```
25
26 The first command, `cargo new`, takes the name of the project (`guessing_game`)
27 as the first argument. The second command changes to the new project’s
28 directory.
29
30 Look at the generated *Cargo.toml* file:
31
32 <!-- manual-regeneration
33 cd listings/ch02-guessing-game-tutorial
34 rm -rf no-listing-01-cargo-new
35 cargo new no-listing-01-cargo-new --name guessing_game
36 cd no-listing-01-cargo-new
37 cargo run > output.txt 2>&1
38 cd ../../..
39 -->
40
41 <span class="filename">Filename: Cargo.toml</span>
42
43 ```toml
44 {{#include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/Cargo.toml}}
45 ```
46
47 As you saw in Chapter 1, `cargo new` generates a “Hello, world!” program for
48 you. Check out the *src/main.rs* file:
49
50 <span class="filename">Filename: src/main.rs</span>
51
52 ```rust
53 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/src/main.rs}}
54 ```
55
56 Now let’s compile this “Hello, world!” program and run it in the same step
57 using the `cargo run` command:
58
59 ```console
60 {{#include ../listings/ch02-guessing-game-tutorial/no-listing-01-cargo-new/output.txt}}
61 ```
62
63 The `run` command comes in handy when you need to rapidly iterate on a project,
64 as we’ll do in this game, quickly testing each iteration before moving on to
65 the next one.
66
67 Reopen the *src/main.rs* file. You’ll be writing all the code in this file.
68
69 ## Processing a Guess
70
71 The first part of the guessing game program will ask for user input, process
72 that input, and check that the input is in the expected form. To start, we’ll
73 allow the player to input a guess. Enter the code in Listing 2-1 into
74 *src/main.rs*.
75
76 <span class="filename">Filename: src/main.rs</span>
77
78 ```rust,ignore
79 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:all}}
80 ```
81
82 <span class="caption">Listing 2-1: Code that gets a guess from the user and
83 prints it</span>
84
85 This code contains a lot of information, so let’s go over it line by line. To
86 obtain user input and then print the result as output, we need to bring the
87 `io` input/output library into scope. The `io` library comes from the standard
88 library, known as `std`:
89
90 ```rust,ignore
91 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:io}}
92 ```
93
94 By default, Rust has a set of items defined in the standard library that it
95 brings into the scope of every program. This set is called the *prelude*, and
96 you can see everything in it [in the standard library documentation][prelude].
97
98 If a type you want to use isn’t in the prelude, you have to bring that type
99 into scope explicitly with a `use` statement. Using the `std::io` library
100 provides you with a number of useful features, including the ability to accept
101 user input.
102
103 As you saw in Chapter 1, the `main` function is the entry point into the
104 program:
105
106 ```rust,ignore
107 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:main}}
108 ```
109
110 The `fn` syntax declares a new function; the parentheses, `()`, indicate there
111 are no parameters; and the curly bracket, `{`, starts the body of the function.
112
113 As you also learned in Chapter 1, `println!` is a macro that prints a string to
114 the screen:
115
116 ```rust,ignore
117 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print}}
118 ```
119
120 This code is printing a prompt stating what the game is and requesting input
121 from the user.
122
123 ### Storing Values with Variables
124
125 Next, we’ll create a *variable* to store the user input, like this:
126
127 ```rust,ignore
128 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:string}}
129 ```
130
131 Now the program is getting interesting! There’s a lot going on in this little
132 line. We use the `let` statement to create the variable. Here’s another example:
133
134 ```rust,ignore
135 let apples = 5;
136 ```
137
138 This line creates a new variable named `apples` and binds it to the value 5. In
139 Rust, variables are immutable by default, meaning once we give the variable a
140 value, the value won’t change. We’ll be discussing this concept in detail in
141 the [“Variables and Mutability”][variables-and-mutability]<!-- ignore -->
142 section in Chapter 3. To make a variable mutable, we add `mut` before the
143 variable name:
144
145 ```rust,ignore
146 let apples = 5; // immutable
147 let mut bananas = 5; // mutable
148 ```
149
150 > Note: The `//` syntax starts a comment that continues until the end of the
151 > line. Rust ignores everything in comments. We’ll discuss comments in more
152 > detail in [Chapter 3][comments]<!-- ignore -->.
153
154 Returning to the guessing game program, you now know that `let mut guess` will
155 introduce a mutable variable named `guess`. The equal sign (`=`) tells Rust we
156 want to bind something to the variable now. On the right of the equal sign is
157 the value that `guess` is bound to, which is the result of calling
158 `String::new`, a function that returns a new instance of a `String`.
159 [`String`][string]<!-- ignore --> is a string type provided by the standard
160 library that is a growable, UTF-8 encoded bit of text.
161
162 The `::` syntax in the `::new` line indicates that `new` is an associated
163 function of the `String` type. An *associated function* is a function that’s
164 implemented on a type, in this case `String`. This `new` function creates a
165 new, empty string. You’ll find a `new` function on many types because it’s a
166 common name for a function that makes a new value of some kind.
167
168 In full, the `let mut guess = String::new();` line has created a mutable
169 variable that is currently bound to a new, empty instance of a `String`. Whew!
170
171 ### Receiving User Input
172
173 Recall that we included the input/output functionality from the standard
174 library with `use std::io;` on the first line of the program. Now we’ll call
175 the `stdin` function from the `io` module, which will allow us to handle user
176 input:
177
178 ```rust,ignore
179 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:read}}
180 ```
181
182 If we hadn’t imported the `io` library with `use std::io;` at the beginning of
183 the program, we could still use the function by writing this function call as
184 `std::io::stdin`. The `stdin` function returns an instance of
185 [`std::io::Stdin`][iostdin]<!-- ignore -->, which is a type that represents a
186 handle to the standard input for your terminal.
187
188 Next, the line `.read_line(&mut guess)` calls the [`read_line`][read_line]<!--
189 ignore --> method on the standard input handle to get input from the user.
190 We’re also passing `&mut guess` as the argument to `read_line` to tell it what
191 string to store the user input in. The full job of `read_line` is to take
192 whatever the user types into standard input and append that into a string
193 (without overwriting its contents), so we therefore pass that string as an
194 argument. The string argument needs to be mutable so the method can change the
195 string’s content.
196
197 The `&` indicates that this argument is a *reference*, which gives you a way to
198 let multiple parts of your code access one piece of data without needing to
199 copy that data into memory multiple times. References are a complex feature,
200 and one of Rust’s major advantages is how safe and easy it is to use
201 references. You don’t need to know a lot of those details to finish this
202 program. For now, all you need to know is that, like variables, references are
203 immutable by default. Hence, you need to write `&mut guess` rather than
204 `&guess` to make it mutable. (Chapter 4 will explain references more
205 thoroughly.)
206
207 <!-- Old heading. Do not remove or links may break. -->
208 <a id="handling-potential-failure-with-the-result-type"></a>
209
210 ### Handling Potential Failure with `Result`
211
212 We’re still working on this line of code. We’re now discussing a third line of
213 text, but note that it’s still part of a single logical line of code. The next
214 part is this method:
215
216 ```rust,ignore
217 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:expect}}
218 ```
219
220 We could have written this code as:
221
222 ```rust,ignore
223 io::stdin().read_line(&mut guess).expect("Failed to read line");
224 ```
225
226 However, one long line is difficult to read, so it’s best to divide it. It’s
227 often wise to introduce a newline and other whitespace to help break up long
228 lines when you call a method with the `.method_name()` syntax. Now let’s
229 discuss what this line does.
230
231 As mentioned earlier, `read_line` puts whatever the user enters into the string
232 we pass to it, but it also returns a `Result` value. [`Result`][result]<!--
233 ignore --> is an [*enumeration*][enums]<!-- ignore -->, often called an *enum*,
234 which is a type that can be in one of multiple possible states. We call each
235 possible state a *variant*.
236
237 [Chapter 6][enums]<!-- ignore --> will cover enums in more detail. The purpose
238 of these `Result` types is to encode error-handling information.
239
240 `Result`’s variants are `Ok` and `Err`. The `Ok` variant indicates the
241 operation was successful, and inside `Ok` is the successfully generated value.
242 The `Err` variant means the operation failed, and `Err` contains information
243 about how or why the operation failed.
244
245 Values of the `Result` type, like values of any type, have methods defined on
246 them. An instance of `Result` has an [`expect` method][expect]<!-- ignore -->
247 that you can call. If this instance of `Result` is an `Err` value, `expect`
248 will cause the program to crash and display the message that you passed as an
249 argument to `expect`. If the `read_line` method returns an `Err`, it would
250 likely be the result of an error coming from the underlying operating system.
251 If this instance of `Result` is an `Ok` value, `expect` will take the return
252 value that `Ok` is holding and return just that value to you so you can use it.
253 In this case, that value is the number of bytes in the user’s input.
254
255 If you don’t call `expect`, the program will compile, but you’ll get a warning:
256
257 ```console
258 {{#include ../listings/ch02-guessing-game-tutorial/no-listing-02-without-expect/output.txt}}
259 ```
260
261 Rust warns that you haven’t used the `Result` value returned from `read_line`,
262 indicating that the program hasn’t handled a possible error.
263
264 The right way to suppress the warning is to actually write error-handling code,
265 but in our case we just want to crash this program when a problem occurs, so we
266 can use `expect`. You’ll learn about recovering from errors in [Chapter
267 9][recover]<!-- ignore -->.
268
269 ### Printing Values with `println!` Placeholders
270
271 Aside from the closing curly bracket, there’s only one more line to discuss in
272 the code so far:
273
274 ```rust,ignore
275 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-01/src/main.rs:print_guess}}
276 ```
277
278 This line prints the string that now contains the user’s input. The `{}` set of
279 curly brackets is a placeholder: think of `{}` as little crab pincers that hold
280 a value in place. When printing the value of a variable, the variable name can
281 go inside the curly brackets. When printing the result of evaluating an
282 expression, place empty curly brackets in the format string, then follow the
283 format string with a comma-separated list of expressions to print in each empty
284 curly bracket placeholder in the same order. Printing a variable and the result
285 of an expression in one call to `println!` would look like this:
286
287 ```rust
288 let x = 5;
289 let y = 10;
290
291 println!("x = {x} and y + 2 = {}", y + 2);
292 ```
293
294 This code would print `x = 5 and y = 12`.
295
296 ### Testing the First Part
297
298 Let’s test the first part of the guessing game. Run it using `cargo run`:
299
300 <!-- manual-regeneration
301 cd listings/ch02-guessing-game-tutorial/listing-02-01/
302 cargo clean
303 cargo run
304 input 6 -->
305
306 ```console
307 $ cargo run
308 Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
309 Finished dev [unoptimized + debuginfo] target(s) in 6.44s
310 Running `target/debug/guessing_game`
311 Guess the number!
312 Please input your guess.
313 6
314 You guessed: 6
315 ```
316
317 At this point, the first part of the game is done: we’re getting input from the
318 keyboard and then printing it.
319
320 ## Generating a Secret Number
321
322 Next, we need to generate a secret number that the user will try to guess. The
323 secret number should be different every time so the game is fun to play more
324 than once. We’ll use a random number between 1 and 100 so the game isn’t too
325 difficult. Rust doesn’t yet include random number functionality in its standard
326 library. However, the Rust team does provide a [`rand` crate][randcrate] with
327 said functionality.
328
329 ### Using a Crate to Get More Functionality
330
331 Remember that a crate is a collection of Rust source code files. The project
332 we’ve been building is a *binary crate*, which is an executable. The `rand`
333 crate is a *library crate*, which contains code that is intended to be used in
334 other programs and can’t be executed on its own.
335
336 Cargo’s coordination of external crates is where Cargo really shines. Before we
337 can write code that uses `rand`, we need to modify the *Cargo.toml* file to
338 include the `rand` crate as a dependency. Open that file now and add the
339 following line to the bottom, beneath the `[dependencies]` section header that
340 Cargo created for you. Be sure to specify `rand` exactly as we have here, with
341 this version number, or the code examples in this tutorial may not work:
342
343 <!-- When updating the version of `rand` used, also update the version of
344 `rand` used in these files so they all match:
345 * ch07-04-bringing-paths-into-scope-with-the-use-keyword.md
346 * ch14-03-cargo-workspaces.md
347 -->
348
349 <span class="filename">Filename: Cargo.toml</span>
350
351 ```toml
352 {{#include ../listings/ch02-guessing-game-tutorial/listing-02-02/Cargo.toml:8:}}
353 ```
354
355 In the *Cargo.toml* file, everything that follows a header is part of that
356 section that continues until another section starts. In `[dependencies]` you
357 tell Cargo which external crates your project depends on and which versions of
358 those crates you require. In this case, we specify the `rand` crate with the
359 semantic version specifier `0.8.5`. Cargo understands [Semantic
360 Versioning][semver]<!-- ignore --> (sometimes called *SemVer*), which is a
361 standard for writing version numbers. The specifier `0.8.5` is actually
362 shorthand for `^0.8.5`, which means any version that is at least 0.8.5 but
363 below 0.9.0.
364
365 Cargo considers these versions to have public APIs compatible with version
366 0.8.5, and this specification ensures you’ll get the latest patch release that
367 will still compile with the code in this chapter. Any version 0.9.0 or greater
368 is not guaranteed to have the same API as what the following examples use.
369
370 Now, without changing any of the code, let’s build the project, as shown in
371 Listing 2-2.
372
373 <!-- manual-regeneration
374 cd listings/ch02-guessing-game-tutorial/listing-02-02/
375 rm Cargo.lock
376 cargo clean
377 cargo build -->
378
379 ```console
380 $ cargo build
381 Updating crates.io index
382 Downloaded rand v0.8.5
383 Downloaded libc v0.2.127
384 Downloaded getrandom v0.2.7
385 Downloaded cfg-if v1.0.0
386 Downloaded ppv-lite86 v0.2.16
387 Downloaded rand_chacha v0.3.1
388 Downloaded rand_core v0.6.3
389 Compiling libc v0.2.127
390 Compiling getrandom v0.2.7
391 Compiling cfg-if v1.0.0
392 Compiling ppv-lite86 v0.2.16
393 Compiling rand_core v0.6.3
394 Compiling rand_chacha v0.3.1
395 Compiling rand v0.8.5
396 Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
397 Finished dev [unoptimized + debuginfo] target(s) in 2.53s
398 ```
399
400 <span class="caption">Listing 2-2: The output from running `cargo build` after
401 adding the rand crate as a dependency</span>
402
403 You may see different version numbers (but they will all be compatible with the
404 code, thanks to SemVer!) and different lines (depending on the operating
405 system), and the lines may be in a different order.
406
407 When we include an external dependency, Cargo fetches the latest versions of
408 everything that dependency needs from the *registry*, which is a copy of data
409 from [Crates.io][cratesio]. Crates.io is where people in the Rust ecosystem
410 post their open source Rust projects for others to use.
411
412 After updating the registry, Cargo checks the `[dependencies]` section and
413 downloads any crates listed that aren’t already downloaded. In this case,
414 although we only listed `rand` as a dependency, Cargo also grabbed other crates
415 that `rand` depends on to work. After downloading the crates, Rust compiles
416 them and then compiles the project with the dependencies available.
417
418 If you immediately run `cargo build` again without making any changes, you
419 won’t get any output aside from the `Finished` line. Cargo knows it has already
420 downloaded and compiled the dependencies, and you haven’t changed anything
421 about them in your *Cargo.toml* file. Cargo also knows that you haven’t changed
422 anything about your code, so it doesn’t recompile that either. With nothing to
423 do, it simply exits.
424
425 If you open the *src/main.rs* file, make a trivial change, and then save it and
426 build again, you’ll only see two lines of output:
427
428 <!-- manual-regeneration
429 cd listings/ch02-guessing-game-tutorial/listing-02-02/
430 touch src/main.rs
431 cargo build -->
432
433 ```console
434 $ cargo build
435 Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
436 Finished dev [unoptimized + debuginfo] target(s) in 2.53 secs
437 ```
438
439 These lines show that Cargo only updates the build with your tiny change to the
440 *src/main.rs* file. Your dependencies haven’t changed, so Cargo knows it can
441 reuse what it has already downloaded and compiled for those.
442
443 #### Ensuring Reproducible Builds with the *Cargo.lock* File
444
445 Cargo has a mechanism that ensures you can rebuild the same artifact every time
446 you or anyone else builds your code: Cargo will use only the versions of the
447 dependencies you specified until you indicate otherwise. For example, say that
448 next week version 0.8.6 of the `rand` crate comes out, and that version
449 contains an important bug fix, but it also contains a regression that will
450 break your code. To handle this, Rust creates the *Cargo.lock* file the first
451 time you run `cargo build`, so we now have this in the *guessing_game*
452 directory.
453
454 When you build a project for the first time, Cargo figures out all the versions
455 of the dependencies that fit the criteria and then writes them to the
456 *Cargo.lock* file. When you build your project in the future, Cargo will see
457 that the *Cargo.lock* file exists and will use the versions specified there
458 rather than doing all the work of figuring out versions again. This lets you
459 have a reproducible build automatically. In other words, your project will
460 remain at 0.8.5 until you explicitly upgrade, thanks to the *Cargo.lock* file.
461 Because the *Cargo.lock* file is important for reproducible builds, it’s often
462 checked into source control with the rest of the code in your project.
463
464 #### Updating a Crate to Get a New Version
465
466 When you *do* want to update a crate, Cargo provides the command `update`,
467 which will ignore the *Cargo.lock* file and figure out all the latest versions
468 that fit your specifications in *Cargo.toml*. Cargo will then write those
469 versions to the *Cargo.lock* file. Otherwise, by default, Cargo will only look
470 for versions greater than 0.8.5 and less than 0.9.0. If the `rand` crate has
471 released the two new versions 0.8.6 and 0.9.0, you would see the following if
472 you ran `cargo update`:
473
474 <!-- manual-regeneration
475 cd listings/ch02-guessing-game-tutorial/listing-02-02/
476 cargo update
477 assuming there is a new 0.8.x version of rand; otherwise use another update
478 as a guide to creating the hypothetical output shown here -->
479
480 ```console
481 $ cargo update
482 Updating crates.io index
483 Updating rand v0.8.5 -> v0.8.6
484 ```
485
486 Cargo ignores the 0.9.0 release. At this point, you would also notice a change
487 in your *Cargo.lock* file noting that the version of the `rand` crate you are
488 now using is 0.8.6. To use `rand` version 0.9.0 or any version in the 0.9.*x*
489 series, you’d have to update the *Cargo.toml* file to look like this instead:
490
491 ```toml
492 [dependencies]
493 rand = "0.9.0"
494 ```
495
496 The next time you run `cargo build`, Cargo will update the registry of crates
497 available and reevaluate your `rand` requirements according to the new version
498 you have specified.
499
500 There’s a lot more to say about [Cargo][doccargo]<!-- ignore --> and [its
501 ecosystem][doccratesio]<!-- ignore -->, which we’ll discuss in Chapter 14, but
502 for now, that’s all you need to know. Cargo makes it very easy to reuse
503 libraries, so Rustaceans are able to write smaller projects that are assembled
504 from a number of packages.
505
506 ### Generating a Random Number
507
508 Let’s start using `rand` to generate a number to guess. The next step is to
509 update *src/main.rs*, as shown in Listing 2-3.
510
511 <span class="filename">Filename: src/main.rs</span>
512
513 ```rust,ignore
514 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-03/src/main.rs:all}}
515 ```
516
517 <span class="caption">Listing 2-3: Adding code to generate a random
518 number</span>
519
520 First we add the line `use rand::Rng;`. The `Rng` trait defines methods that
521 random number generators implement, and this trait must be in scope for us to
522 use those methods. Chapter 10 will cover traits in detail.
523
524 Next, we’re adding two lines in the middle. In the first line, we call the
525 `rand::thread_rng` function that gives us the particular random number
526 generator we’re going to use: one that is local to the current thread of
527 execution and is seeded by the operating system. Then we call the `gen_range`
528 method on the random number generator. This method is defined by the `Rng`
529 trait that we brought into scope with the `use rand::Rng;` statement. The
530 `gen_range` method takes a range expression as an argument and generates a
531 random number in the range. The kind of range expression we’re using here takes
532 the form `start..=end` and is inclusive on the lower and upper bounds, so we
533 need to specify `1..=100` to request a number between 1 and 100.
534
535 > Note: You won’t just know which traits to use and which methods and functions
536 > to call from a crate, so each crate has documentation with instructions for
537 > using it. Another neat feature of Cargo is that running the `cargo doc
538 > --open` command will build documentation provided by all your dependencies
539 > locally and open it in your browser. If you’re interested in other
540 > functionality in the `rand` crate, for example, run `cargo doc --open` and
541 > click `rand` in the sidebar on the left.
542
543 The second new line prints the secret number. This is useful while we’re
544 developing the program to be able to test it, but we’ll delete it from the
545 final version. It’s not much of a game if the program prints the answer as soon
546 as it starts!
547
548 Try running the program a few times:
549
550 <!-- manual-regeneration
551 cd listings/ch02-guessing-game-tutorial/listing-02-03/
552 cargo run
553 4
554 cargo run
555 5
556 -->
557
558 ```console
559 $ cargo run
560 Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
561 Finished dev [unoptimized + debuginfo] target(s) in 2.53s
562 Running `target/debug/guessing_game`
563 Guess the number!
564 The secret number is: 7
565 Please input your guess.
566 4
567 You guessed: 4
568
569 $ cargo run
570 Finished dev [unoptimized + debuginfo] target(s) in 0.02s
571 Running `target/debug/guessing_game`
572 Guess the number!
573 The secret number is: 83
574 Please input your guess.
575 5
576 You guessed: 5
577 ```
578
579 You should get different random numbers, and they should all be numbers between
580 1 and 100. Great job!
581
582 ## Comparing the Guess to the Secret Number
583
584 Now that we have user input and a random number, we can compare them. That step
585 is shown in Listing 2-4. Note that this code won’t compile just yet, as we will
586 explain.
587
588 <span class="filename">Filename: src/main.rs</span>
589
590 ```rust,ignore,does_not_compile
591 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-04/src/main.rs:here}}
592 ```
593
594 <span class="caption">Listing 2-4: Handling the possible return values of
595 comparing two numbers</span>
596
597 First we add another `use` statement, bringing a type called
598 `std::cmp::Ordering` into scope from the standard library. The `Ordering` type
599 is another enum and has the variants `Less`, `Greater`, and `Equal`. These are
600 the three outcomes that are possible when you compare two values.
601
602 Then we add five new lines at the bottom that use the `Ordering` type. The
603 `cmp` method compares two values and can be called on anything that can be
604 compared. It takes a reference to whatever you want to compare with: here it’s
605 comparing `guess` to `secret_number`. Then it returns a variant of the
606 `Ordering` enum we brought into scope with the `use` statement. We use a
607 [`match`][match]<!-- ignore --> expression to decide what to do next based on
608 which variant of `Ordering` was returned from the call to `cmp` with the values
609 in `guess` and `secret_number`.
610
611 A `match` expression is made up of *arms*. An arm consists of a *pattern* to
612 match against, and the code that should be run if the value given to `match`
613 fits that arm’s pattern. Rust takes the value given to `match` and looks
614 through each arm’s pattern in turn. Patterns and the `match` construct are
615 powerful Rust features: they let you express a variety of situations your code
616 might encounter and they make sure you handle them all. These features will be
617 covered in detail in Chapter 6 and Chapter 18, respectively.
618
619 Let’s walk through an example with the `match` expression we use here. Say that
620 the user has guessed 50 and the randomly generated secret number this time is
621 38.
622
623 When the code compares 50 to 38, the `cmp` method will return
624 `Ordering::Greater` because 50 is greater than 38. The `match` expression gets
625 the `Ordering::Greater` value and starts checking each arm’s pattern. It looks
626 at the first arm’s pattern, `Ordering::Less`, and sees that the value
627 `Ordering::Greater` does not match `Ordering::Less`, so it ignores the code in
628 that arm and moves to the next arm. The next arm’s pattern is
629 `Ordering::Greater`, which *does* match `Ordering::Greater`! The associated
630 code in that arm will execute and print `Too big!` to the screen. The `match`
631 expression ends after the first successful match, so it won’t look at the last
632 arm in this scenario.
633
634 However, the code in Listing 2-4 won’t compile yet. Let’s try it:
635
636 <!--
637 The error numbers in this output should be that of the code **WITHOUT** the
638 anchor or snip comments
639 -->
640
641 ```console
642 {{#include ../listings/ch02-guessing-game-tutorial/listing-02-04/output.txt}}
643 ```
644
645 The core of the error states that there are *mismatched types*. Rust has a
646 strong, static type system. However, it also has type inference. When we wrote
647 `let mut guess = String::new()`, Rust was able to infer that `guess` should be
648 a `String` and didn’t make us write the type. The `secret_number`, on the other
649 hand, is a number type. A few of Rust’s number types can have a value between 1
650 and 100: `i32`, a 32-bit number; `u32`, an unsigned 32-bit number; `i64`, a
651 64-bit number; as well as others. Unless otherwise specified, Rust defaults to
652 an `i32`, which is the type of `secret_number` unless you add type information
653 elsewhere that would cause Rust to infer a different numerical type. The reason
654 for the error is that Rust cannot compare a string and a number type.
655
656 Ultimately, we want to convert the `String` the program reads as input into a
657 real number type so we can compare it numerically to the secret number. We do
658 so by adding this line to the `main` function body:
659
660 <span class="filename">Filename: src/main.rs</span>
661
662 ```rust,ignore
663 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/src/main.rs:here}}
664 ```
665
666 The line is:
667
668 ```rust,ignore
669 let guess: u32 = guess.trim().parse().expect("Please type a number!");
670 ```
671
672 We create a variable named `guess`. But wait, doesn’t the program already have
673 a variable named `guess`? It does, but helpfully Rust allows us to shadow the
674 previous value of `guess` with a new one. *Shadowing* lets us reuse the `guess`
675 variable name rather than forcing us to create two unique variables, such as
676 `guess_str` and `guess`, for example. We’ll cover this in more detail in
677 [Chapter 3][shadowing]<!-- ignore -->, but for now, know that this feature is
678 often used when you want to convert a value from one type to another type.
679
680 We bind this new variable to the expression `guess.trim().parse()`. The `guess`
681 in the expression refers to the original `guess` variable that contained the
682 input as a string. The `trim` method on a `String` instance will eliminate any
683 whitespace at the beginning and end, which we must do to be able to compare the
684 string to the `u32`, which can only contain numerical data. The user must press
685 <span class="keystroke">enter</span> to satisfy `read_line` and input their
686 guess, which adds a newline character to the string. For example, if the user
687 types <span class="keystroke">5</span> and presses <span
688 class="keystroke">enter</span>, `guess` looks like this: `5\n`. The `\n`
689 represents “newline.” (On Windows, pressing <span
690 class="keystroke">enter</span> results in a carriage return and a newline,
691 `\r\n`.) The `trim` method eliminates `\n` or `\r\n`, resulting in just `5`.
692
693 The [`parse` method on strings][parse]<!-- ignore --> converts a string to
694 another type. Here, we use it to convert from a string to a number. We need to
695 tell Rust the exact number type we want by using `let guess: u32`. The colon
696 (`:`) after `guess` tells Rust we’ll annotate the variable’s type. Rust has a
697 few built-in number types; the `u32` seen here is an unsigned, 32-bit integer.
698 It’s a good default choice for a small positive number. You’ll learn about
699 other number types in [Chapter 3][integers]<!-- ignore -->.
700
701 Additionally, the `u32` annotation in this example program and the comparison
702 with `secret_number` means Rust will infer that `secret_number` should be a
703 `u32` as well. So now the comparison will be between two values of the same
704 type!
705
706 The `parse` method will only work on characters that can logically be converted
707 into numbers and so can easily cause errors. If, for example, the string
708 contained `A👍%`, there would be no way to convert that to a number. Because it
709 might fail, the `parse` method returns a `Result` type, much as the `read_line`
710 method does (discussed earlier in [“Handling Potential Failure with
711 `Result`”](#handling-potential-failure-with-result)<!-- ignore-->). We’ll treat
712 this `Result` the same way by using the `expect` method again. If `parse`
713 returns an `Err` `Result` variant because it couldn’t create a number from the
714 string, the `expect` call will crash the game and print the message we give it.
715 If `parse` can successfully convert the string to a number, it will return the
716 `Ok` variant of `Result`, and `expect` will return the number that we want from
717 the `Ok` value.
718
719 Let’s run the program now:
720
721 <!-- manual-regeneration
722 cd listings/ch02-guessing-game-tutorial/no-listing-03-convert-string-to-number/
723 cargo run
724 76
725 -->
726
727 ```console
728 $ cargo run
729 Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
730 Finished dev [unoptimized + debuginfo] target(s) in 0.43s
731 Running `target/debug/guessing_game`
732 Guess the number!
733 The secret number is: 58
734 Please input your guess.
735 76
736 You guessed: 76
737 Too big!
738 ```
739
740 Nice! Even though spaces were added before the guess, the program still figured
741 out that the user guessed 76. Run the program a few times to verify the
742 different behavior with different kinds of input: guess the number correctly,
743 guess a number that is too high, and guess a number that is too low.
744
745 We have most of the game working now, but the user can make only one guess.
746 Let’s change that by adding a loop!
747
748 ## Allowing Multiple Guesses with Looping
749
750 The `loop` keyword creates an infinite loop. We’ll add a loop to give users
751 more chances at guessing the number:
752
753 <span class="filename">Filename: src/main.rs</span>
754
755 ```rust,ignore
756 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-04-looping/src/main.rs:here}}
757 ```
758
759 As you can see, we’ve moved everything from the guess input prompt onward into
760 a loop. Be sure to indent the lines inside the loop another four spaces each
761 and run the program again. The program will now ask for another guess forever,
762 which actually introduces a new problem. It doesn’t seem like the user can quit!
763
764 The user could always interrupt the program by using the keyboard shortcut
765 <span class="keystroke">ctrl-c</span>. But there’s another way to escape this
766 insatiable monster, as mentioned in the `parse` discussion in [“Comparing the
767 Guess to the Secret Number”](#comparing-the-guess-to-the-secret-number)<!--
768 ignore -->: if the user enters a non-number answer, the program will crash. We
769 can take advantage of that to allow the user to quit, as shown here:
770
771 <!-- manual-regeneration
772 cd listings/ch02-guessing-game-tutorial/no-listing-04-looping/
773 cargo run
774 (too small guess)
775 (too big guess)
776 (correct guess)
777 quit
778 -->
779
780 ```console
781 $ cargo run
782 Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
783 Finished dev [unoptimized + debuginfo] target(s) in 1.50s
784 Running `target/debug/guessing_game`
785 Guess the number!
786 The secret number is: 59
787 Please input your guess.
788 45
789 You guessed: 45
790 Too small!
791 Please input your guess.
792 60
793 You guessed: 60
794 Too big!
795 Please input your guess.
796 59
797 You guessed: 59
798 You win!
799 Please input your guess.
800 quit
801 thread 'main' panicked at 'Please type a number!: ParseIntError { kind: InvalidDigit }', src/main.rs:28:47
802 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
803 ```
804
805 Typing `quit` will quit the game, but as you’ll notice, so will entering any
806 other non-number input. This is suboptimal, to say the least; we want the game
807 to also stop when the correct number is guessed.
808
809 ### Quitting After a Correct Guess
810
811 Let’s program the game to quit when the user wins by adding a `break` statement:
812
813 <span class="filename">Filename: src/main.rs</span>
814
815 ```rust,ignore
816 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/no-listing-05-quitting/src/main.rs:here}}
817 ```
818
819 Adding the `break` line after `You win!` makes the program exit the loop when
820 the user guesses the secret number correctly. Exiting the loop also means
821 exiting the program, because the loop is the last part of `main`.
822
823 ### Handling Invalid Input
824
825 To further refine the game’s behavior, rather than crashing the program when
826 the user inputs a non-number, let’s make the game ignore a non-number so the
827 user can continue guessing. We can do that by altering the line where `guess`
828 is converted from a `String` to a `u32`, as shown in Listing 2-5.
829
830 <span class="filename">Filename: src/main.rs</span>
831
832 ```rust,ignore
833 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-05/src/main.rs:here}}
834 ```
835
836 <span class="caption">Listing 2-5: Ignoring a non-number guess and asking for
837 another guess instead of crashing the program</span>
838
839 We switch from an `expect` call to a `match` expression to move from crashing
840 on an error to handling the error. Remember that `parse` returns a `Result`
841 type and `Result` is an enum that has the variants `Ok` and `Err`. We’re using
842 a `match` expression here, as we did with the `Ordering` result of the `cmp`
843 method.
844
845 If `parse` is able to successfully turn the string into a number, it will
846 return an `Ok` value that contains the resultant number. That `Ok` value will
847 match the first arm’s pattern, and the `match` expression will just return the
848 `num` value that `parse` produced and put inside the `Ok` value. That number
849 will end up right where we want it in the new `guess` variable we’re creating.
850
851 If `parse` is *not* able to turn the string into a number, it will return an
852 `Err` value that contains more information about the error. The `Err` value
853 does not match the `Ok(num)` pattern in the first `match` arm, but it does
854 match the `Err(_)` pattern in the second arm. The underscore, `_`, is a
855 catchall value; in this example, we’re saying we want to match all `Err`
856 values, no matter what information they have inside them. So the program will
857 execute the second arm’s code, `continue`, which tells the program to go to the
858 next iteration of the `loop` and ask for another guess. So, effectively, the
859 program ignores all errors that `parse` might encounter!
860
861 Now everything in the program should work as expected. Let’s try it:
862
863 <!-- manual-regeneration
864 cd listings/ch02-guessing-game-tutorial/listing-02-05/
865 cargo run
866 (too small guess)
867 (too big guess)
868 foo
869 (correct guess)
870 -->
871
872 ```console
873 $ cargo run
874 Compiling guessing_game v0.1.0 (file:///projects/guessing_game)
875 Finished dev [unoptimized + debuginfo] target(s) in 4.45s
876 Running `target/debug/guessing_game`
877 Guess the number!
878 The secret number is: 61
879 Please input your guess.
880 10
881 You guessed: 10
882 Too small!
883 Please input your guess.
884 99
885 You guessed: 99
886 Too big!
887 Please input your guess.
888 foo
889 Please input your guess.
890 61
891 You guessed: 61
892 You win!
893 ```
894
895 Awesome! With one tiny final tweak, we will finish the guessing game. Recall
896 that the program is still printing the secret number. That worked well for
897 testing, but it ruins the game. Let’s delete the `println!` that outputs the
898 secret number. Listing 2-6 shows the final code.
899
900 <span class="filename">Filename: src/main.rs</span>
901
902 ```rust,ignore
903 {{#rustdoc_include ../listings/ch02-guessing-game-tutorial/listing-02-06/src/main.rs}}
904 ```
905
906 <span class="caption">Listing 2-6: Complete guessing game code</span>
907
908 At this point, you’ve successfully built the guessing game. Congratulations!
909
910 ## Summary
911
912 This project was a hands-on way to introduce you to many new Rust concepts:
913 `let`, `match`, functions, the use of external crates, and more. In the next
914 few chapters, you’ll learn about these concepts in more detail. Chapter 3
915 covers concepts that most programming languages have, such as variables, data
916 types, and functions, and shows how to use them in Rust. Chapter 4 explores
917 ownership, a feature that makes Rust different from other languages. Chapter 5
918 discusses structs and method syntax, and Chapter 6 explains how enums work.
919
920 [prelude]: ../std/prelude/index.html
921 [variables-and-mutability]: ch03-01-variables-and-mutability.html#variables-and-mutability
922 [comments]: ch03-04-comments.html
923 [string]: ../std/string/struct.String.html
924 [iostdin]: ../std/io/struct.Stdin.html
925 [read_line]: ../std/io/struct.Stdin.html#method.read_line
926 [result]: ../std/result/enum.Result.html
927 [enums]: ch06-00-enums.html
928 [expect]: ../std/result/enum.Result.html#method.expect
929 [recover]: ch09-02-recoverable-errors-with-result.html
930 [randcrate]: https://crates.io/crates/rand
931 [semver]: http://semver.org
932 [cratesio]: https://crates.io/
933 [doccargo]: http://doc.crates.io
934 [doccratesio]: http://doc.crates.io/crates-io.html
935 [match]: ch06-02-match.html
936 [shadowing]: ch03-01-variables-and-mutability.html#shadowing
937 [parse]: ../std/primitive.str.html#method.parse
938 [integers]: ch03-02-data-types.html#integer-types