Skip to content

Commit 2b2e9fc

Browse files
authored
Rollup merge of rust-lang#37483 - xfix:patch-1, r=steveklabnik
Match guessing game output to newest language version Cargo now informs that it has finished, and there is new error format.
2 parents b2db5b9 + 5704a53 commit 2b2e9fc

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

src/doc/book/guessing-game.md

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ has a command that does that for us. Let’s give it a shot:
1919
```bash
2020
$ cd ~/projects
2121
$ cargo new guessing_game --bin
22+
Created binary (application) `guessing_game` project
2223
$ cd guessing_game
2324
```
2425

@@ -51,6 +52,7 @@ Let’s try compiling what Cargo gave us:
5152
```{bash}
5253
$ cargo build
5354
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
55+
Finished debug [unoptimized + debuginfo] target(s) in 0.53 secs
5456
```
5557

5658
Excellent! Open up your `src/main.rs` again. We’ll be writing all of
@@ -61,6 +63,7 @@ Remember the `run` command from last chapter? Try it out again here:
6163
```bash
6264
$ cargo run
6365
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
66+
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
6467
Running `target/debug/guessing_game`
6568
Hello, world!
6669
```
@@ -282,10 +285,13 @@ we’ll get a warning:
282285
```bash
283286
$ cargo build
284287
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
285-
src/main.rs:10:5: 10:39 warning: unused result which must be used,
286-
#[warn(unused_must_use)] on by default
287-
src/main.rs:10 io::stdin().read_line(&mut guess);
288-
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
288+
warning: unused result which must be used, #[warn(unused_must_use)] on by default
289+
--> src/main.rs:10:5
290+
|
291+
10 | io::stdin().read_line(&mut guess);
292+
| ^
293+
294+
Finished debug [unoptimized + debuginfo] target(s) in 0.42 secs
289295
```
290296

291297
Rust warns us that we haven’t used the `Result` value. This warning comes from
@@ -321,6 +327,7 @@ Anyway, that’s the tour. We can run what we have with `cargo run`:
321327
```bash
322328
$ cargo run
323329
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
330+
Finished debug [unoptimized + debuginfo] target(s) in 0.44 secs
324331
Running `target/debug/guessing_game`
325332
Guess the number!
326333
Please input your guess.
@@ -373,11 +380,12 @@ Now, without changing any of our code, let’s build our project:
373380
```bash
374381
$ cargo build
375382
Updating registry `https://github.com/rust-lang/crates.io-index`
376-
Downloading rand v0.3.8
377-
Downloading libc v0.1.6
378-
Compiling libc v0.1.6
379-
Compiling rand v0.3.8
383+
Downloading rand v0.3.14
384+
Downloading libc v0.2.17
385+
Compiling libc v0.2.17
386+
Compiling rand v0.3.14
380387
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
388+
Finished debug [unoptimized + debuginfo] target(s) in 5.88 secs
381389
```
382390

383391
(You may see different versions, of course.)
@@ -399,22 +407,24 @@ If we run `cargo build` again, we’ll get different output:
399407

400408
```bash
401409
$ cargo build
410+
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
402411
```
403412

404-
That’s right, no output! Cargo knows that our project has been built, and that
413+
That’s right, nothing was done! Cargo knows that our project has been built, and that
405414
all of its dependencies are built, and so there’s no reason to do all that
406415
stuff. With nothing to do, it simply exits. If we open up `src/main.rs` again,
407-
make a trivial change, and then save it again, we’ll only see one line:
416+
make a trivial change, and then save it again, we’ll only see two lines:
408417

409418
```bash
410419
$ cargo build
411420
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
421+
Finished debug [unoptimized + debuginfo] target(s) in 0.45 secs
412422
```
413423

414424
So, we told Cargo we wanted any `0.3.x` version of `rand`, and so it fetched the latest
415-
version at the time this was written, `v0.3.8`. But what happens when next
416-
week, version `v0.3.9` comes out, with an important bugfix? While getting
417-
bugfixes is important, what if `0.3.9` contains a regression that breaks our
425+
version at the time this was written, `v0.3.14`. But what happens when next
426+
week, version `v0.3.15` comes out, with an important bugfix? While getting
427+
bugfixes is important, what if `0.3.15` contains a regression that breaks our
418428
code?
419429

420430
The answer to this problem is the `Cargo.lock` file you’ll now find in your
@@ -423,11 +433,11 @@ figures out all of the versions that fit your criteria, and then writes them
423433
to the `Cargo.lock` file. When you build your project in the future, Cargo
424434
will see that the `Cargo.lock` file exists, and then use that specific version
425435
rather than do all the work of figuring out versions again. This lets you
426-
have a repeatable build automatically. In other words, we’ll stay at `0.3.8`
436+
have a repeatable build automatically. In other words, we’ll stay at `0.3.14`
427437
until we explicitly upgrade, and so will anyone who we share our code with,
428438
thanks to the lock file.
429439

430-
What about when we _do_ want to use `v0.3.9`? Cargo has another command,
440+
What about when we _do_ want to use `v0.3.15`? Cargo has another command,
431441
`update`, which says ‘ignore the lock, figure out all the latest versions that
432442
fit what we’ve specified. If that works, write those versions out to the lock
433443
file’. But, by default, Cargo will only look for versions larger than `0.3.0`
@@ -510,13 +520,15 @@ Try running our new program a few times:
510520
```bash
511521
$ cargo run
512522
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
523+
Finished debug [unoptimized + debuginfo] target(s) in 0.55 secs
513524
Running `target/debug/guessing_game`
514525
Guess the number!
515526
The secret number is: 7
516527
Please input your guess.
517528
4
518529
You guessed: 4
519530
$ cargo run
531+
Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
520532
Running `target/debug/guessing_game`
521533
Guess the number!
522534
The secret number is: 83
@@ -618,15 +630,20 @@ I did mention that this won’t quite compile yet, though. Let’s try it:
618630
```bash
619631
$ cargo build
620632
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
621-
src/main.rs:28:21: 28:35 error: mismatched types:
622-
expected `&collections::string::String`,
623-
found `&_`
624-
(expected struct `collections::string::String`,
625-
found integral variable) [E0308]
626-
src/main.rs:28 match guess.cmp(&secret_number) {
627-
^~~~~~~~~~~~~~
633+
error[E0308]: mismatched types
634+
--> src/main.rs:23:21
635+
|
636+
23 | match guess.cmp(&secret_number) {
637+
| ^^^^^^^^^^^^^^ expected struct `std::string::String`, found integral variable
638+
|
639+
= note: expected type `&std::string::String`
640+
= note: found type `&{integer}`
641+
628642
error: aborting due to previous error
629-
Could not compile `guessing_game`.
643+
644+
error: Could not compile `guessing_game`.
645+
646+
To learn more, run the command again with --verbose.
630647
```
631648
632649
Whew! This is a big error. The core of it is that we have ‘mismatched types’.
@@ -722,6 +739,7 @@ Let’s try our program out!
722739
```bash
723740
$ cargo run
724741
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
742+
Finished debug [unoptimized + debuginfo] target(s) in 0.57 secs
725743
Running `target/guessing_game`
726744
Guess the number!
727745
The secret number is: 58
@@ -785,6 +803,7 @@ and quit. Observe:
785803
```bash
786804
$ cargo run
787805
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
806+
Finished debug [unoptimized + debuginfo] target(s) in 0.58 secs
788807
Running `target/guessing_game`
789808
Guess the number!
790809
The secret number is: 59
@@ -919,6 +938,7 @@ Now we should be good! Let’s try:
919938
```bash
920939
$ cargo run
921940
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
941+
Finished debug [unoptimized + debuginfo] target(s) in 0.57 secs
922942
Running `target/guessing_game`
923943
Guess the number!
924944
The secret number is: 61

0 commit comments

Comments
 (0)