@@ -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.
@@ -373,11 +380,12 @@ Now, without changing any of our code, let’s build our project:
373
380
``` bash
374
381
$ cargo build
375
382
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
380
387
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
388
+ Finished debug [unoptimized + debuginfo] target(s) in 5.88 secs
381
389
```
382
390
383
391
(You may see different versions, of course.)
@@ -399,22 +407,24 @@ If we run `cargo build` again, we’ll get different output:
399
407
400
408
``` bash
401
409
$ cargo build
410
+ Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
402
411
```
403
412
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
405
414
all of its dependencies are built, and so there’s no reason to do all that
406
415
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 :
408
417
409
418
``` bash
410
419
$ cargo build
411
420
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
421
+ Finished debug [unoptimized + debuginfo] target(s) in 0.45 secs
412
422
```
413
423
414
424
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
418
428
code?
419
429
420
430
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
423
433
to the ` Cargo.lock ` file. When you build your project in the future, Cargo
424
434
will see that the ` Cargo.lock ` file exists, and then use that specific version
425
435
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 `
427
437
until we explicitly upgrade, and so will anyone who we share our code with,
428
438
thanks to the lock file.
429
439
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,
431
441
` update ` , which says ‘ignore the lock, figure out all the latest versions that
432
442
fit what we’ve specified. If that works, write those versions out to the lock
433
443
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:
510
520
``` bash
511
521
$ cargo run
512
522
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
523
+ Finished debug [unoptimized + debuginfo] target(s) in 0.55 secs
513
524
Running ` target/debug/guessing_game`
514
525
Guess the number!
515
526
The secret number is: 7
516
527
Please input your guess.
517
528
4
518
529
You guessed: 4
519
530
$ cargo run
531
+ Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs
520
532
Running ` target/debug/guessing_game`
521
533
Guess the number!
522
534
The secret number is: 83
@@ -618,15 +630,20 @@ I did mention that this won’t quite compile yet, though. Let’s try it:
618
630
``` bash
619
631
$ cargo build
620
632
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
+
628
642
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.
630
647
` ` `
631
648
632
649
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!
722
739
` ` ` bash
723
740
$ cargo run
724
741
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
742
+ Finished debug [unoptimized + debuginfo] target(s) in 0.57 secs
725
743
Running ` target/guessing_game`
726
744
Guess the number!
727
745
The secret number is: 58
@@ -785,6 +803,7 @@ and quit. Observe:
785
803
` ` ` bash
786
804
$ cargo run
787
805
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
806
+ Finished debug [unoptimized + debuginfo] target(s) in 0.58 secs
788
807
Running ` target/guessing_game`
789
808
Guess the number!
790
809
The secret number is: 59
@@ -919,6 +938,7 @@ Now we should be good! Let’s try:
919
938
```bash
920
939
$ cargo run
921
940
Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game)
941
+ Finished debug [unoptimized + debuginfo] target(s) in 0.57 secs
922
942
Running `target/guessing_game`
923
943
Guess the number!
924
944
The secret number is: 61
0 commit comments