@@ -19,6 +19,7 @@ has a command that does that for us. Let’s give it a shot:
19
19
``` bash
20
20
$ cd ~ /projects
21
21
$ cargo new guessing_game --bin
22
+ Created binary (application) ` guessing_game` project
22
23
$ cd guessing_game
23
24
```
24
25
@@ -51,6 +52,7 @@ Let’s try compiling what Cargo gave us:
51
52
``` {bash}
52
53
$ cargo build
53
54
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
55
+ Finished debug [unoptimized + debuginfo] target(s) in 0.53 secs
54
56
```
55
57
56
58
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:
61
63
``` bash
62
64
$ cargo run
63
65
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
66
+ Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
64
67
Running ` target/debug/guessing_game`
65
68
Hello, world!
66
69
```
@@ -282,10 +285,13 @@ we’ll get a warning:
282
285
``` bash
283
286
$ cargo build
284
287
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
289
295
```
290
296
291
297
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`:
321
327
``` bash
322
328
$ cargo run
323
329
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
330
+ Finished debug [unoptimized + debuginfo] target(s) in 0.44 secs
324
331
Running ` target/debug/guessing_game`
325
332
Guess the number!
326
333
Please input your guess.
@@ -374,11 +381,12 @@ Now, without changing any of our code, let’s build our project:
374
381
``` bash
375
382
$ cargo build
376
383
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
381
388
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
389
+ Finished debug [unoptimized + debuginfo] target(s) in 5.88 secs
382
390
```
383
391
384
392
(You may see different versions, of course.)
@@ -400,22 +408,24 @@ If we run `cargo build` again, we’ll get different output:
400
408
401
409
``` bash
402
410
$ cargo build
411
+ Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
403
412
```
404
413
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
406
415
all of its dependencies are built, and so there’s no reason to do all that
407
416
stuff. 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 :
409
418
410
419
``` bash
411
420
$ cargo build
412
421
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
422
+ Finished debug [unoptimized + debuginfo] target(s) in 0.45 secs
413
423
```
414
424
415
425
So, 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
419
429
code?
420
430
421
431
The 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
424
434
to the ` Cargo.lock ` file. When you build your project in the future, Cargo
425
435
will see that the ` Cargo.lock ` file exists, and then use that specific version
426
436
rather 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 `
428
438
until we explicitly upgrade, and so will anyone who we share our code with,
429
439
thanks to the lock file.
430
440
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,
432
442
` update ` , which says ‘ignore the lock, figure out all the latest versions that
433
443
fit what we’ve specified. If that works, write those versions out to the lock
434
444
file’. 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:
511
521
``` bash
512
522
$ cargo run
513
523
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
524
+ Finished debug [unoptimized + debuginfo] target(s) in 0.55 secs
514
525
Running ` target/debug/guessing_game`
515
526
Guess the number!
516
527
The secret number is: 7
517
528
Please input your guess.
518
529
4
519
530
You guessed: 4
520
531
$ cargo run
532
+ Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
521
533
Running ` target/debug/guessing_game`
522
534
Guess the number!
523
535
The secret number is: 83
@@ -619,15 +631,20 @@ I did mention that this won’t quite compile yet, though. Let’s try it:
619
631
``` bash
620
632
$ cargo build
621
633
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
+
629
643
error: 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.
631
648
` ` `
632
649
633
650
Whew! 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!
723
740
` ` ` bash
724
741
$ cargo run
725
742
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
743
+ Finished debug [unoptimized + debuginfo] target(s) in 0.57 secs
726
744
Running ` target/guessing_game`
727
745
Guess the number!
728
746
The secret number is: 58
@@ -786,6 +804,7 @@ and quit. Observe:
786
804
` ` ` bash
787
805
$ cargo run
788
806
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
807
+ Finished debug [unoptimized + debuginfo] target(s) in 0.58 secs
789
808
Running ` target/guessing_game`
790
809
Guess the number!
791
810
The secret number is: 59
@@ -920,6 +939,7 @@ Now we should be good! Let’s try:
920
939
```bash
921
940
$ cargo run
922
941
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
942
+ Finished debug [unoptimized + debuginfo] target(s) in 0.57 secs
923
943
Running `target/guessing_game`
924
944
Guess the number!
925
945
The secret number is: 61
0 commit comments