]> git.lizzy.rs Git - rust.git/blob - src/doc/trpl/guessing-game.md
Rollup merge of #21357 - kimroen:patch-1, r=sanxiyn
[rust.git] / src / doc / trpl / guessing-game.md
1 % Guessing Game
2
3 Okay! We've got the basics of Rust down. Let's write a bigger program.
4
5 For our first project, we'll implement a classic beginner programming problem:
6 the guessing game. Here's how it works: Our program will generate a random
7 integer between one and a hundred. It will then prompt us to enter a guess.
8 Upon entering our guess, it will tell us if we're too low or too high. Once we
9 guess correctly, it will congratulate us. Sound good?
10
11 ## Set up
12
13 Let's set up a new project. Go to your projects directory. Remember how we
14 had to create our directory structure and a `Cargo.toml` for `hello_world`? Cargo
15 has a command that does that for us. Let's give it a shot:
16
17 ```{bash}
18 $ cd ~/projects
19 $ cargo new guessing_game --bin
20 $ cd guessing_game
21 ```
22
23 We pass the name of our project to `cargo new`, and then the `--bin` flag,
24 since we're making a binary, rather than a library.
25
26 Check out the generated `Cargo.toml`:
27
28 ```toml
29 [package]
30
31 name = "guessing_game"
32 version = "0.0.1"
33 authors = ["Your Name <you@example.com>"]
34 ```
35
36 Cargo gets this information from your environment. If it's not correct, go ahead
37 and fix that.
38
39 Finally, Cargo generated a "Hello, world!" for us. Check out `src/main.rs`:
40
41 ```{rust}
42 fn main() {
43     println!("Hello, world!")
44 }
45 ```
46
47 Let's try compiling what Cargo gave us:
48
49 ```{bash}
50 $ cargo build
51    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
52 ```
53
54 Excellent! Open up your `src/main.rs` again. We'll be writing all of
55 our code in this file. We'll talk about multiple-file projects later on in the
56 guide.
57
58 Before we move on, let me show you one more Cargo command: `run`. `cargo run`
59 is kind of like `cargo build`, but it also then runs the produced executable.
60 Try it out:
61
62 ```bash
63 $ cargo run
64    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
65      Running `target/guessing_game`
66 Hello, world!
67 ```
68
69 Great! The `run` command comes in handy when you need to rapidly iterate on a project.
70 Our game is just such a project, we need to quickly test each iteration before moving on to the next one.
71
72 ## Processing a Guess
73
74 Let's get to it! The first thing we need to do for our guessing game is
75 allow our player to input a guess. Put this in your `src/main.rs`:
76
77 ```{rust,no_run}
78 use std::io;
79
80 fn main() {
81     println!("Guess the number!");
82
83     println!("Please input your guess.");
84
85     let input = io::stdin().read_line()
86                            .ok()
87                            .expect("Failed to read line");
88
89     println!("You guessed: {}", input);
90 }
91 ```
92
93 You've seen this code before, when we talked about standard input. We
94 import the `std::io` module with `use`, and then our `main` function contains
95 our program's logic. We print a little message announcing the game, ask the
96 user to input a guess, get their input, and then print it out.
97
98 Because we talked about this in the section on standard I/O, I won't go into
99 more details here. If you need a refresher, go re-read that section.
100
101 ## Generating a secret number
102
103 Next, we need to generate a secret number. To do that, we need to use Rust's
104 random number generation, which we haven't talked about yet. Rust includes a
105 bunch of interesting functions in its standard library. If you need a bit of
106 code, it's possible that it's already been written for you! In this case,
107 we do know that Rust has random number generation, but we don't know how to
108 use it.
109
110 Enter the docs. Rust has a page specifically to document the standard library.
111 You can find that page [here](../std/index.html). There's a lot of information on
112 that page, but the best part is the search bar. Right up at the top, there's
113 a box that you can enter in a search term. The search is pretty primitive
114 right now, but is getting better all the time. If you type "random" in that
115 box, the page will update to [this one](../std/index.html?search=random). The very
116 first result is a link to [`std::rand::random`](../std/rand/fn.random.html). If we
117 click on that result, we'll be taken to its documentation page.
118
119 This page shows us a few things: the type signature of the function, some
120 explanatory text, and then an example. Let's try to modify our code to add in the
121 `random` function and see what happens:
122
123 ```{rust,ignore}
124 use std::io;
125 use std::rand;
126
127 fn main() {
128     println!("Guess the number!");
129
130     let secret_number = (rand::random() % 100) + 1; // secret_number: i32
131
132     println!("The secret number is: {}", secret_number);
133
134     println!("Please input your guess.");
135
136     let input = io::stdin().read_line()
137                            .ok()
138                            .expect("Failed to read line");
139
140
141     println!("You guessed: {}", input);
142 }
143 ```
144
145 The first thing we changed was to `use std::rand`, as the docs
146 explained.  We then added in a `let` expression to create a variable binding
147 named `secret_number`, and we printed out its result.
148
149 Also, you may wonder why we are using `%` on the result of `rand::random()`.
150 This operator is called *modulo*, and it returns the remainder of a division.
151 By taking the modulo of the result of `rand::random()`, we're limiting the
152 values to be between 0 and 99. Then, we add one to the result, making it from 1
153 to 100. Using modulo can give you a very, very small bias in the result, but
154 for this example, it is not important.
155
156 Let's try to compile this using `cargo build`:
157
158 ```bash
159 $ cargo build
160    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
161 src/main.rs:7:26: 7:34 error: the type of this value must be known in this context
162 src/main.rs:7     let secret_number = (rand::random() % 100) + 1;
163                                        ^~~~~~~~
164 error: aborting due to previous error
165 ```
166
167 It didn't work! Rust says "the type of this value must be known in this
168 context." What's up with that? Well, as it turns out, `rand::random()` can
169 generate many kinds of random values, not just integers. And in this case, Rust
170 isn't sure what kind of value `random()` should generate. So we have to help
171 it. With number literals, we can just add an `i32` onto the end to tell Rust they're
172 integers, but that does not work with functions. There's a different syntax,
173 and it looks like this:
174
175 ```{rust,ignore}
176 rand::random::<i32>();
177 ```
178
179 This says "please give me a random `i32` value." We can change our code to use
180 this hint:
181
182 ```{rust,no_run}
183 use std::io;
184 use std::rand;
185
186 fn main() {
187     println!("Guess the number!");
188
189     let secret_number = (rand::random::<i32>() % 100) + 1;
190
191     println!("The secret number is: {}", secret_number);
192
193     println!("Please input your guess.");
194
195     let input = io::stdin().read_line()
196                            .ok()
197                            .expect("Failed to read line");
198
199
200     println!("You guessed: {}", input);
201 }
202 ```
203
204 Try running our new program a few times:
205
206 ```bash
207 $ cargo run
208    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
209      Running `target/guessing_game`
210 Guess the number!
211 The secret number is: 7
212 Please input your guess.
213 4
214 You guessed: 4
215 $ ./target/guessing_game
216 Guess the number!
217 The secret number is: 83
218 Please input your guess.
219 5
220 You guessed: 5
221 $ ./target/guessing_game
222 Guess the number!
223 The secret number is: -29
224 Please input your guess.
225 42
226 You guessed: 42
227 ```
228
229 Wait. Negative 29? We wanted a number between one and a hundred! We have two
230 options here: we can either ask `random()` to generate an unsigned integer, which
231 can only be positive, or we can use the `abs()` function. Let's go with the
232 unsigned integer approach. If we want a random positive number, we should ask for
233 a random positive number. Our code looks like this now:
234
235 ```{rust,no_run}
236 use std::io;
237 use std::rand;
238
239 fn main() {
240     println!("Guess the number!");
241
242     let secret_number = (rand::random::<u32>() % 100) + 1;
243
244     println!("The secret number is: {}", secret_number);
245
246     println!("Please input your guess.");
247
248     let input = io::stdin().read_line()
249                            .ok()
250                            .expect("Failed to read line");
251
252
253     println!("You guessed: {}", input);
254 }
255 ```
256
257 And trying it out:
258
259 ```bash
260 $ cargo run
261    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
262      Running `target/guessing_game`
263 Guess the number!
264 The secret number is: 57
265 Please input your guess.
266 3
267 You guessed: 3
268 ```
269
270 Great! Next up: let's compare our guess to the secret guess.
271
272 ## Comparing guesses
273
274 If you remember, earlier in the guide, we made a `cmp` function that compared
275 two numbers. Let's add that in, along with a `match` statement to compare our
276 guess to the secret number:
277
278 ```{rust,ignore}
279 use std::io;
280 use std::rand;
281 use std::cmp::Ordering;
282
283 fn main() {
284     println!("Guess the number!");
285
286     let secret_number = (rand::random::<u32>() % 100) + 1;
287
288     println!("The secret number is: {}", secret_number);
289
290     println!("Please input your guess.");
291
292     let input = io::stdin().read_line()
293                            .ok()
294                            .expect("Failed to read line");
295
296
297     println!("You guessed: {}", input);
298
299     match cmp(input, secret_number) {
300         Ordering::Less => println!("Too small!"),
301         Ordering::Greater => println!("Too big!"),
302         Ordering::Equal => println!("You win!"),
303     }
304 }
305
306 fn cmp(a: i32, b: i32) -> Ordering {
307     if a < b { Ordering::Less }
308     else if a > b { Ordering::Greater }
309     else { Ordering::Equal }
310 }
311 ```
312
313 If we try to compile, we'll get some errors:
314
315 ```bash
316 $ cargo build
317    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
318 src/main.rs:20:15: 20:20 error: mismatched types: expected `i32` but found `collections::string::String` (expected i32 but found struct collections::string::String)
319 src/main.rs:20     match cmp(input, secret_number) {
320                              ^~~~~
321 src/main.rs:20:22: 20:35 error: mismatched types: expected `i32` but found `u32` (expected i32 but found u32)
322 src/main.rs:20     match cmp(input, secret_number) {
323                                     ^~~~~~~~~~~~~
324 error: aborting due to 2 previous errors
325 ```
326
327 This often happens when writing Rust programs, and is one of Rust's greatest
328 strengths. You try out some code, see if it compiles, and Rust tells you that
329 you've done something wrong. In this case, our `cmp` function works on integers,
330 but we've given it unsigned integers. In this case, the fix is easy, because
331 we wrote the `cmp` function! Let's change it to take `u32`s:
332
333 ```{rust,ignore}
334 use std::io;
335 use std::rand;
336 use std::cmp::Ordering;
337
338 fn main() {
339     println!("Guess the number!");
340
341     let secret_number = (rand::random::<u32>() % 100) + 1;
342
343     println!("The secret number is: {}", secret_number);
344
345     println!("Please input your guess.");
346
347     let input = io::stdin().read_line()
348                            .ok()
349                            .expect("Failed to read line");
350
351
352     println!("You guessed: {}", input);
353
354     match cmp(input, secret_number) {
355         Ordering::Less => println!("Too small!"),
356         Ordering::Greater => println!("Too big!"),
357         Ordering::Equal => println!("You win!"),
358     }
359 }
360
361 fn cmp(a: u32, b: u32) -> Ordering {
362     if a < b { Ordering::Less }
363     else if a > b { Ordering::Greater }
364     else { Ordering::Equal }
365 }
366 ```
367
368 And try compiling again:
369
370 ```bash
371 $ cargo build
372    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
373 src/main.rs:20:15: 20:20 error: mismatched types: expected `u32` but found `collections::string::String` (expected u32 but found struct collections::string::String)
374 src/main.rs:20     match cmp(input, secret_number) {
375                              ^~~~~
376 error: aborting due to previous error
377 ```
378
379 This error is similar to the last one: we expected to get a `u32`, but we got
380 a `String` instead! That's because our `input` variable is coming from the
381 standard input, and you can guess anything. Try it:
382
383 ```bash
384 $ ./target/guessing_game
385 Guess the number!
386 The secret number is: 73
387 Please input your guess.
388 hello
389 You guessed: hello
390 ```
391
392 Oops! Also, you'll note that we just ran our program even though it didn't compile.
393 This works because the older version we did successfully compile was still lying
394 around. Gotta be careful!
395
396 Anyway, we have a `String`, but we need a `u32`. What to do? Well, there's
397 a function for that:
398
399 ```{rust,ignore}
400 let input = io::stdin().read_line()
401                        .ok()
402                        .expect("Failed to read line");
403 let input_num: Option<u32> = input.parse();
404 ```
405
406 The `parse` function takes in a `&str` value and converts it into something.
407 We tell it what kind of something with a type hint. Remember our type hint with
408 `random()`? It looked like this:
409
410 ```{rust,ignore}
411 rand::random::<u32>();
412 ```
413
414 There's an alternate way of providing a hint too, and that's declaring the type
415 in a `let`:
416
417 ```{rust,ignore}
418 let x: u32 = rand::random();
419 ```
420
421 In this case, we say `x` is a `u32` explicitly, so Rust is able to properly
422 tell `random()` what to generate. In a similar fashion, both of these work:
423
424 ```{rust,ignore}
425 let input_num = "5".parse::<u32>(); // input_num: Option<u32>
426 let input_num: Option<u32> = "5".parse(); // input_num: Option<u32>
427 ```
428
429 Anyway, with us now converting our input to a number, our code looks like this:
430
431 ```{rust,ignore}
432 use std::io;
433 use std::rand;
434 use std::cmp::Ordering;
435
436 fn main() {
437     println!("Guess the number!");
438
439     let secret_number = (rand::random::<u32>() % 100) + 1;
440
441     println!("The secret number is: {}", secret_number);
442
443     println!("Please input your guess.");
444
445     let input = io::stdin().read_line()
446                            .ok()
447                            .expect("Failed to read line");
448     let input_num: Option<u32> = input.parse();
449
450     println!("You guessed: {}", input_num);
451
452     match cmp(input_num, secret_number) {
453         Ordering::Less => println!("Too small!"),
454         Ordering::Greater => println!("Too big!"),
455         Ordering::Equal => println!("You win!"),
456     }
457 }
458
459 fn cmp(a: u32, b: u32) -> Ordering {
460     if a < b { Ordering::Less }
461     else if a > b { Ordering::Greater }
462     else { Ordering::Equal }
463 }
464 ```
465
466 Let's try it out!
467
468 ```bash
469 $ cargo build
470    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
471 src/main.rs:22:15: 22:24 error: mismatched types: expected `u32` but found `core::option::Option<u32>` (expected u32 but found enum core::option::Option)
472 src/main.rs:22     match cmp(input_num, secret_number) {
473                              ^~~~~~~~~
474 error: aborting due to previous error
475 ```
476
477 Oh yeah! Our `input_num` has the type `Option<u32>`, rather than `u32`. We
478 need to unwrap the Option. If you remember from before, `match` is a great way
479 to do that. Try this code:
480
481 ```{rust,no_run}
482 use std::io;
483 use std::rand;
484 use std::cmp::Ordering;
485
486 fn main() {
487     println!("Guess the number!");
488
489     let secret_number = (rand::random::<u32>() % 100) + 1;
490
491     println!("The secret number is: {}", secret_number);
492
493     println!("Please input your guess.");
494
495     let input = io::stdin().read_line()
496                            .ok()
497                            .expect("Failed to read line");
498     let input_num: Option<u32> = input.parse();
499
500     let num = match input_num {
501         Some(num) => num,
502         None => {
503             println!("Please input a number!");
504             return;
505         }
506     };
507
508
509     println!("You guessed: {}", num);
510
511     match cmp(num, secret_number) {
512         Ordering::Less => println!("Too small!"),
513         Ordering::Greater => println!("Too big!"),
514         Ordering::Equal => println!("You win!"),
515     }
516 }
517
518 fn cmp(a: u32, b: u32) -> Ordering {
519     if a < b { Ordering::Less }
520     else if a > b { Ordering::Greater }
521     else { Ordering::Equal }
522 }
523 ```
524
525 We use a `match` to either give us the `u32` inside of the `Option`, or else
526 print an error message and return. Let's give this a shot:
527
528 ```bash
529 $ cargo run
530    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
531      Running `target/guessing_game`
532 Guess the number!
533 The secret number is: 17
534 Please input your guess.
535 5
536 Please input a number!
537 ```
538
539 Uh, what? But we did!
540
541 ... actually, we didn't. See, when you get a line of input from `stdin()`,
542 you get all the input. Including the `\n` character from you pressing Enter.
543 Therefore, `parse()` sees the string `"5\n"` and says "nope, that's not a
544 number; there's non-number stuff in there!" Luckily for us, `&str`s have an easy
545 method we can use defined on them: `trim()`. One small modification, and our
546 code looks like this:
547
548 ```{rust,no_run}
549 use std::io;
550 use std::rand;
551 use std::cmp::Ordering;
552
553 fn main() {
554     println!("Guess the number!");
555
556     let secret_number = (rand::random::<u32>() % 100) + 1;
557
558     println!("The secret number is: {}", secret_number);
559
560     println!("Please input your guess.");
561
562     let input = io::stdin().read_line()
563                            .ok()
564                            .expect("Failed to read line");
565     let input_num: Option<u32> = input.trim().parse();
566
567     let num = match input_num {
568         Some(num) => num,
569         None => {
570             println!("Please input a number!");
571             return;
572         }
573     };
574
575
576     println!("You guessed: {}", num);
577
578     match cmp(num, secret_number) {
579         Ordering::Less => println!("Too small!"),
580         Ordering::Greater => println!("Too big!"),
581         Ordering::Equal => println!("You win!"),
582     }
583 }
584
585 fn cmp(a: u32, b: u32) -> Ordering {
586     if a < b { Ordering::Less }
587     else if a > b { Ordering::Greater }
588     else { Ordering::Equal }
589 }
590 ```
591
592 Let's try it!
593
594 ```bash
595 $ cargo run
596    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
597      Running `target/guessing_game`
598 Guess the number!
599 The secret number is: 58
600 Please input your guess.
601   76
602 You guessed: 76
603 Too big!
604 ```
605
606 Nice! You can see I even added spaces before my guess, and it still figured
607 out that I guessed 76. Run the program a few times, and verify that guessing
608 the number works, as well as guessing a number too small.
609
610 The Rust compiler helped us out quite a bit there! This technique is called
611 "leaning on the compiler", and it's often useful when working on some code.
612 Let the error messages help guide you towards the correct types.
613
614 Now we've got most of the game working, but we can only make one guess. Let's
615 change that by adding loops!
616
617 ## Looping
618
619 As we already discussed, the `loop` keyword gives us an infinite loop.
620 Let's add that in:
621
622 ```{rust,no_run}
623 use std::io;
624 use std::rand;
625 use std::cmp::Ordering;
626
627 fn main() {
628     println!("Guess the number!");
629
630     let secret_number = (rand::random::<u32>() % 100) + 1;
631
632     println!("The secret number is: {}", secret_number);
633
634     loop {
635
636         println!("Please input your guess.");
637
638         let input = io::stdin().read_line()
639                                .ok()
640                                .expect("Failed to read line");
641         let input_num: Option<u32> = input.trim().parse();
642
643         let num = match input_num {
644             Some(num) => num,
645             None => {
646                 println!("Please input a number!");
647                 return;
648             }
649         };
650
651
652         println!("You guessed: {}", num);
653
654         match cmp(num, secret_number) {
655             Ordering::Less => println!("Too small!"),
656             Ordering::Greater => println!("Too big!"),
657             Ordering::Equal => println!("You win!"),
658         }
659     }
660 }
661
662 fn cmp(a: u32, b: u32) -> Ordering {
663     if a < b { Ordering::Less }
664     else if a > b { Ordering::Greater }
665     else { Ordering::Equal }
666 }
667 ```
668
669 And try it out. But wait, didn't we just add an infinite loop? Yup. Remember
670 that `return`? If we give a non-number answer, we'll `return` and quit. Observe:
671
672 ```bash
673 $ cargo run
674    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
675      Running `target/guessing_game`
676 Guess the number!
677 The secret number is: 59
678 Please input your guess.
679 45
680 You guessed: 45
681 Too small!
682 Please input your guess.
683 60
684 You guessed: 60
685 Too big!
686 Please input your guess.
687 59
688 You guessed: 59
689 You win!
690 Please input your guess.
691 quit
692 Please input a number!
693 ```
694
695 Ha! `quit` actually quits. As does any other non-number input. Well, this is
696 suboptimal to say the least. First, let's actually quit when you win the game:
697
698 ```{rust,no_run}
699 use std::io;
700 use std::rand;
701 use std::cmp::Ordering;
702
703 fn main() {
704     println!("Guess the number!");
705
706     let secret_number = (rand::random::<u32>() % 100) + 1;
707
708     println!("The secret number is: {}", secret_number);
709
710     loop {
711
712         println!("Please input your guess.");
713
714         let input = io::stdin().read_line()
715                                .ok()
716                                .expect("Failed to read line");
717         let input_num: Option<u32> = input.trim().parse();
718
719         let num = match input_num {
720             Some(num) => num,
721             None => {
722                 println!("Please input a number!");
723                 return;
724             }
725         };
726
727
728         println!("You guessed: {}", num);
729
730         match cmp(num, secret_number) {
731             Ordering::Less => println!("Too small!"),
732             Ordering::Greater => println!("Too big!"),
733             Ordering::Equal => {
734                 println!("You win!");
735                 return;
736             },
737         }
738     }
739 }
740
741 fn cmp(a: u32, b: u32) -> Ordering {
742     if a < b { Ordering::Less }
743     else if a > b { Ordering::Greater }
744     else { Ordering::Equal }
745 }
746 ```
747
748 By adding the `return` line after the `You win!`, we'll exit the program when
749 we win. We have just one more tweak to make: when someone inputs a non-number,
750 we don't want to quit, we just want to ignore it. Change that `return` to
751 `continue`:
752
753
754 ```{rust,no_run}
755 use std::io;
756 use std::rand;
757 use std::cmp::Ordering;
758
759 fn main() {
760     println!("Guess the number!");
761
762     let secret_number = (rand::random::<u32>() % 100) + 1;
763
764     println!("The secret number is: {}", secret_number);
765
766     loop {
767
768         println!("Please input your guess.");
769
770         let input = io::stdin().read_line()
771                                .ok()
772                                .expect("Failed to read line");
773         let input_num: Option<u32> = input.trim().parse();
774
775         let num = match input_num {
776             Some(num) => num,
777             None => {
778                 println!("Please input a number!");
779                 continue;
780             }
781         };
782
783
784         println!("You guessed: {}", num);
785
786         match cmp(num, secret_number) {
787             Ordering::Less => println!("Too small!"),
788             Ordering::Greater => println!("Too big!"),
789             Ordering::Equal => {
790                 println!("You win!");
791                 return;
792             },
793         }
794     }
795 }
796
797 fn cmp(a: u32, b: u32) -> Ordering {
798     if a < b { Ordering::Less }
799     else if a > b { Ordering::Greater }
800     else { Ordering::Equal }
801 }
802 ```
803
804 Now we should be good! Let's try:
805
806 ```bash
807 $ cargo run
808    Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
809      Running `target/guessing_game`
810 Guess the number!
811 The secret number is: 61
812 Please input your guess.
813 10
814 You guessed: 10
815 Too small!
816 Please input your guess.
817 99
818 You guessed: 99
819 Too big!
820 Please input your guess.
821 foo
822 Please input a number!
823 Please input your guess.
824 61
825 You guessed: 61
826 You win!
827 ```
828
829 Awesome! With one tiny last tweak, we have finished the guessing game. Can you
830 think of what it is? That's right, we don't want to print out the secret number.
831 It was good for testing, but it kind of ruins the game. Here's our final source:
832
833 ```{rust,no_run}
834 use std::io;
835 use std::rand;
836 use std::cmp::Ordering;
837
838 fn main() {
839     println!("Guess the number!");
840
841     let secret_number = (rand::random::<u32>() % 100) + 1;
842
843     loop {
844
845         println!("Please input your guess.");
846
847         let input = io::stdin().read_line()
848                                .ok()
849                                .expect("Failed to read line");
850         let input_num: Option<u32> = input.trim().parse();
851
852         let num = match input_num {
853             Some(num) => num,
854             None => {
855                 println!("Please input a number!");
856                 continue;
857             }
858         };
859
860
861         println!("You guessed: {}", num);
862
863         match cmp(num, secret_number) {
864             Ordering::Less => println!("Too small!"),
865             Ordering::Greater => println!("Too big!"),
866             Ordering::Equal => {
867                 println!("You win!");
868                 return;
869             },
870         }
871     }
872 }
873
874 fn cmp(a: u32, b: u32) -> Ordering {
875     if a < b { Ordering::Less }
876     else if a > b { Ordering::Greater }
877     else { Ordering::Equal }
878 }
879 ```
880
881 ## Complete!
882
883 At this point, you have successfully built the Guessing Game! Congratulations!
884
885 You've now learned the basic syntax of Rust. All of this is relatively close to
886 various other programming languages you have used in the past. These
887 fundamental syntactical and semantic elements will form the foundation for the
888 rest of your Rust education.
889
890 Now that you're an expert at the basics, it's time to learn about some of
891 Rust's more unique features.