@@ -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
5658Excellent! 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`
6568Hello, 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
291297Rust 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`
325332Guess the number!
326333Please input your guess.
@@ -374,11 +381,12 @@ Now, without changing any of our code, let’s build our project:
374381``` bash
375382$ cargo build
376383 Updating registry ` https://github.com/rust-lang/crates.io-index`
377- Downloading rand v0.3.8
378- Downloading libc v0.1.6
379- Compiling libc v0.1.6
380- Compiling rand v0.3.8
384+ Downloading rand v0.3.14
385+ Downloading libc v0.2.17
386+ Compiling libc v0.2.17
387+ Compiling rand v0.3.14
381388 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
389+ Finished debug [unoptimized + debuginfo] target(s) in 5.88 secs
382390```
383391
384392(You may see different versions, of course.)
@@ -400,22 +408,24 @@ If we run `cargo build` again, we’ll get different output:
400408
401409``` bash
402410$ cargo build
411+ Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
403412```
404413
405- That’s right, no output ! Cargo knows that our project has been built, and that
414+ That’s right, nothing was done ! Cargo knows that our project has been built, and that
406415all of its dependencies are built, and so there’s no reason to do all that
407416stuff. With nothing to do, it simply exits. If we open up ` src/main.rs ` again,
408- make a trivial change, and then save it again, we’ll only see one line :
417+ make a trivial change, and then save it again, we’ll only see two lines :
409418
410419``` bash
411420$ cargo build
412421 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
422+ Finished debug [unoptimized + debuginfo] target(s) in 0.45 secs
413423```
414424
415425So, we told Cargo we wanted any ` 0.3.x ` version of ` rand ` , and so it fetched the latest
416- version at the time this was written, ` v0.3.8 ` . But what happens when next
417- week, version ` v0.3.9 ` comes out, with an important bugfix? While getting
418- bugfixes is important, what if ` 0.3.9 ` contains a regression that breaks our
426+ version at the time this was written, ` v0.3.14 ` . But what happens when next
427+ week, version ` v0.3.15 ` comes out, with an important bugfix? While getting
428+ bugfixes is important, what if ` 0.3.15 ` contains a regression that breaks our
419429code?
420430
421431The answer to this problem is the ` Cargo.lock ` file you’ll now find in your
@@ -424,11 +434,11 @@ figures out all of the versions that fit your criteria, and then writes them
424434to the ` Cargo.lock ` file. When you build your project in the future, Cargo
425435will see that the ` Cargo.lock ` file exists, and then use that specific version
426436rather than do all the work of figuring out versions again. This lets you
427- have a repeatable build automatically. In other words, we’ll stay at ` 0.3.8 `
437+ have a repeatable build automatically. In other words, we’ll stay at ` 0.3.14 `
428438until we explicitly upgrade, and so will anyone who we share our code with,
429439thanks to the lock file.
430440
431- What about when we _ do_ want to use ` v0.3.9 ` ? Cargo has another command,
441+ What about when we _ do_ want to use ` v0.3.15 ` ? Cargo has another command,
432442` update ` , which says ‘ignore the lock, figure out all the latest versions that
433443fit what we’ve specified. If that works, write those versions out to the lock
434444file’. But, by default, Cargo will only look for versions larger than ` 0.3.0 `
@@ -511,13 +521,15 @@ Try running our new program a few times:
511521``` bash
512522$ cargo run
513523 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
524+ Finished debug [unoptimized + debuginfo] target(s) in 0.55 secs
514525 Running ` target/debug/guessing_game`
515526Guess the number!
516527The secret number is: 7
517528Please input your guess.
5185294
519530You guessed: 4
520531$ cargo run
532+ Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
521533 Running ` target/debug/guessing_game`
522534Guess the number!
523535The secret number is: 83
@@ -619,15 +631,20 @@ I did mention that this won’t quite compile yet, though. Let’s try it:
619631``` bash
620632$ cargo build
621633 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
622- src/main.rs:28:21: 28:35 error: mismatched types:
623- expected ` & collections::string::String` ,
624- found ` & _`
625- (expected struct ` collections::string::String` ,
626- found integral variable) [E0308]
627- src/main.rs:28 match guess.cmp(& secret_number) {
628- ^~~~~~~~~~~~~~
634+ error[E0308]: mismatched types
635+ --> src/main.rs:23:21
636+ |
637+ 23 | match guess.cmp(& secret_number) {
638+ | ^^^^^^^^^^^^^^ expected struct ` std::string::String` , found integral variable
639+ |
640+ = note: expected type ` & std::string::String`
641+ = note: found type ` & {integer}`
642+
629643error: aborting due to previous error
630- Could not compile ` guessing_game` .
644+
645+ error: Could not compile ` guessing_game` .
646+
647+ To learn more, run the command again with --verbose.
631648` ` `
632649
633650Whew! This is a big error. The core of it is that we have ‘mismatched types’.
@@ -723,6 +740,7 @@ Let’s try our program out!
723740` ` ` bash
724741$ cargo run
725742 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
743+ Finished debug [unoptimized + debuginfo] target(s) in 0.57 secs
726744 Running ` target/guessing_game`
727745Guess the number!
728746The secret number is: 58
@@ -786,6 +804,7 @@ and quit. Observe:
786804` ` ` bash
787805$ cargo run
788806 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
807+ Finished debug [unoptimized + debuginfo] target(s) in 0.58 secs
789808 Running ` target/guessing_game`
790809Guess the number!
791810The secret number is: 59
@@ -920,6 +939,7 @@ Now we should be good! Let’s try:
920939```bash
921940$ cargo run
922941 Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
942+ Finished debug [unoptimized + debuginfo] target(s) in 0.57 secs
923943 Running `target/guessing_game`
924944Guess the number!
925945The secret number is: 61
0 commit comments