You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/doc/trpl/guessing-game.md
+39-39Lines changed: 39 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -239,7 +239,7 @@ use std::rand;
239
239
fn main() {
240
240
println!("Guess the number!");
241
241
242
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
242
+
let secret_number = (rand::random::<u32>() % 100) + 1;
243
243
244
244
println!("The secret number is: {}", secret_number);
245
245
@@ -283,7 +283,7 @@ use std::cmp::Ordering;
283
283
fn main() {
284
284
println!("Guess the number!");
285
285
286
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
286
+
let secret_number = (rand::random::<u32>() % 100) + 1;
287
287
288
288
println!("The secret number is: {}", secret_number);
289
289
@@ -318,7 +318,7 @@ $ cargo build
318
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
319
src/main.rs:20 match cmp(input, secret_number) {
320
320
^~~~~
321
-
src/main.rs:20:22: 20:35 error: mismatched types: expected `i32` but found `uint` (expected i32 but found uint)
321
+
src/main.rs:20:22: 20:35 error: mismatched types: expected `i32` but found `u32` (expected i32 but found u32)
322
322
src/main.rs:20 match cmp(input, secret_number) {
323
323
^~~~~~~~~~~~~
324
324
error: aborting due to 2 previous errors
@@ -328,7 +328,7 @@ This often happens when writing Rust programs, and is one of Rust's greatest
328
328
strengths. You try out some code, see if it compiles, and Rust tells you that
329
329
you've done something wrong. In this case, our `cmp`functionworks on integers,
330
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 `uint`s:
331
+
we wrote the `cmp` function! Let's change it to take `u32`s:
332
332
333
333
```{rust,ignore}
334
334
use std::io;
@@ -338,7 +338,7 @@ use std::cmp::Ordering;
338
338
fn main() {
339
339
println!("Guess the number!");
340
340
341
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
341
+
let secret_number = (rand::random::<u32>() % 100) + 1;
342
342
343
343
println!("The secret number is: {}", secret_number);
src/main.rs:20:15: 20:20 error: mismatched types: expected `uint` but found `collections::string::String` (expected uint but found struct collections::string::String)
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
374
src/main.rs:20 match cmp(input, secret_number) {
375
375
^~~~~
376
376
error: aborting due to previous error
377
377
```
378
378
379
-
This error is similar to the last one: we expected to get a `uint`, but we got
379
+
This error is similar to the last one: we expected to get a `u32`, but we got
380
380
a `String` instead! That's because our `input` variable is coming from the
381
381
standard input, and you can guess anything. Try it:
382
382
@@ -393,37 +393,37 @@ Oops! Also, you'll note that we just ran our program even though it didn't compi
393
393
This works because the older version we did successfully compile was still lying
394
394
around. Gotta be careful!
395
395
396
-
Anyway, we have a `String`, but we need a `uint`. What to do? Well, there's
396
+
Anyway, we have a `String`, but we need a `u32`. What to do? Well, there's
397
397
a functionfor that:
398
398
399
399
```{rust,ignore}
400
400
let input = io::stdin().read_line()
401
401
.ok()
402
402
.expect("Failed to read line");
403
-
let input_num: Option<uint> = input.parse();
403
+
let input_num: Option<u32> = input.parse();
404
404
```
405
405
406
406
The `parse`functiontakesin a `&str` value and converts it into something.
407
407
We tell it what kind of something with a type hint. Remember our type hint with
408
408
`random()`? It looked like this:
409
409
410
410
```{rust,ignore}
411
-
rand::random::<uint>();
411
+
rand::random::<u32>();
412
412
```
413
413
414
414
There's an alternate way of providing a hint too, and that's declaring the type
415
415
in a `let`:
416
416
417
417
```{rust,ignore}
418
-
let x: uint = rand::random();
418
+
let x: u32 = rand::random();
419
419
```
420
420
421
-
In this case, we say `x` is a `uint` explicitly, so Rust is able to properly
421
+
In this case, we say `x` is a `u32` explicitly, so Rust is able to properly
422
422
tell `random()` what to generate. In a similar fashion, both of these work:
423
423
424
424
```{rust,ignore}
425
-
let input_num = "5".parse::<uint>(); // input_num: Option<uint>
426
-
let input_num: Option<uint> = "5".parse(); // input_num: Option<uint>
425
+
let input_num = "5".parse::<u32>(); // input_num: Option<u32>
426
+
let input_num: Option<u32> = "5".parse(); // input_num: Option<u32>
427
427
```
428
428
429
429
Anyway, with us now converting our input to a number, our code looks like this:
@@ -436,7 +436,7 @@ use std::cmp::Ordering;
436
436
fn main() {
437
437
println!("Guess the number!");
438
438
439
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
439
+
let secret_number = (rand::random::<u32>() % 100) + 1;
440
440
441
441
println!("The secret number is: {}", secret_number);
src/main.rs:22:15: 22:24 error: mismatched types: expected `uint` but found `core::option::Option<uint>` (expected uint but found enum core::option::Option)
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
472
src/main.rs:22 match cmp(input_num, secret_number) {
473
473
^~~~~~~~~
474
474
error: aborting due to previous error
475
475
```
476
476
477
-
Oh yeah! Our `input_num` has the type `Option<uint>`, rather than `uint`. We
477
+
Oh yeah! Our `input_num` has the type `Option<u32>`, rather than `u32`. We
478
478
need to unwrap the Option. If you remember from before, `match` is a great way
479
479
to do that. Try this code:
480
480
@@ -486,7 +486,7 @@ use std::cmp::Ordering;
486
486
fn main() {
487
487
println!("Guess the number!");
488
488
489
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
489
+
let secret_number = (rand::random::<u32>() % 100) + 1;
490
490
491
491
println!("The secret number is: {}", secret_number);
492
492
@@ -495,7 +495,7 @@ fn main() {
495
495
let input = io::stdin().read_line()
496
496
.ok()
497
497
.expect("Failed to read line");
498
-
let input_num: Option<uint> = input.parse();
498
+
let input_num: Option<u32> = input.parse();
499
499
500
500
let num = match input_num {
501
501
Some(num) => num,
@@ -515,14 +515,14 @@ fn main() {
515
515
}
516
516
}
517
517
518
-
fn cmp(a: uint, b: uint) -> Ordering {
518
+
fn cmp(a: u32, b: u32) -> Ordering {
519
519
if a < b { Ordering::Less }
520
520
else if a > b { Ordering::Greater }
521
521
else { Ordering::Equal }
522
522
}
523
523
```
524
524
525
-
We use a `match` to either give us the `uint` inside of the `Option`, or else
525
+
We use a `match` to either give us the `u32` inside of the `Option`, or else
526
526
print an error message and return. Let's give this a shot:
527
527
528
528
```bash
@@ -553,7 +553,7 @@ use std::cmp::Ordering;
553
553
fn main() {
554
554
println!("Guess the number!");
555
555
556
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
556
+
let secret_number = (rand::random::<u32>() % 100) + 1;
557
557
558
558
println!("The secret number is: {}", secret_number);
559
559
@@ -562,7 +562,7 @@ fn main() {
562
562
let input = io::stdin().read_line()
563
563
.ok()
564
564
.expect("Failed to read line");
565
-
let input_num: Option<uint> = input.trim().parse();
565
+
let input_num: Option<u32> = input.trim().parse();
566
566
567
567
let num = match input_num {
568
568
Some(num) => num,
@@ -582,7 +582,7 @@ fn main() {
582
582
}
583
583
}
584
584
585
-
fn cmp(a: uint, b: uint) -> Ordering {
585
+
fn cmp(a: u32, b: u32) -> Ordering {
586
586
if a < b { Ordering::Less }
587
587
else if a > b { Ordering::Greater }
588
588
else { Ordering::Equal }
@@ -627,7 +627,7 @@ use std::cmp::Ordering;
627
627
fn main() {
628
628
println!("Guess the number!");
629
629
630
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
630
+
let secret_number = (rand::random::<u32>() % 100) + 1;
631
631
632
632
println!("The secret number is: {}", secret_number);
633
633
@@ -638,7 +638,7 @@ fn main() {
638
638
let input = io::stdin().read_line()
639
639
.ok()
640
640
.expect("Failed to read line");
641
-
let input_num: Option<uint> = input.trim().parse();
641
+
let input_num: Option<u32> = input.trim().parse();
642
642
643
643
let num = match input_num {
644
644
Some(num) => num,
@@ -659,7 +659,7 @@ fn main() {
659
659
}
660
660
}
661
661
662
-
fn cmp(a: uint, b: uint) -> Ordering {
662
+
fn cmp(a: u32, b: u32) -> Ordering {
663
663
if a < b { Ordering::Less }
664
664
elseif a > b { Ordering::Greater }
665
665
else { Ordering::Equal }
@@ -703,7 +703,7 @@ use std::cmp::Ordering;
703
703
fn main() {
704
704
println!("Guess the number!");
705
705
706
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
706
+
let secret_number = (rand::random::<u32>() % 100) + 1;
707
707
708
708
println!("The secret number is: {}", secret_number);
709
709
@@ -714,7 +714,7 @@ fn main() {
714
714
let input = io::stdin().read_line()
715
715
.ok()
716
716
.expect("Failed to read line");
717
-
let input_num: Option<uint> = input.trim().parse();
717
+
let input_num: Option<u32> = input.trim().parse();
718
718
719
719
let num = match input_num {
720
720
Some(num) => num,
@@ -738,7 +738,7 @@ fn main() {
738
738
}
739
739
}
740
740
741
-
fn cmp(a: uint, b: uint) -> Ordering {
741
+
fn cmp(a: u32, b: u32) -> Ordering {
742
742
if a < b { Ordering::Less }
743
743
else if a > b { Ordering::Greater }
744
744
else { Ordering::Equal }
@@ -759,7 +759,7 @@ use std::cmp::Ordering;
759
759
fn main() {
760
760
println!("Guess the number!");
761
761
762
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
762
+
let secret_number = (rand::random::<u32>() % 100) + 1;
763
763
764
764
println!("The secret number is: {}", secret_number);
765
765
@@ -770,7 +770,7 @@ fn main() {
770
770
let input = io::stdin().read_line()
771
771
.ok()
772
772
.expect("Failed to read line");
773
-
let input_num: Option<uint> = input.trim().parse();
773
+
let input_num: Option<u32> = input.trim().parse();
774
774
775
775
let num = match input_num {
776
776
Some(num) => num,
@@ -794,7 +794,7 @@ fn main() {
794
794
}
795
795
}
796
796
797
-
fn cmp(a: uint, b: uint) -> Ordering {
797
+
fn cmp(a: u32, b: u32) -> Ordering {
798
798
if a < b { Ordering::Less }
799
799
else if a > b { Ordering::Greater }
800
800
else { Ordering::Equal }
@@ -838,7 +838,7 @@ use std::cmp::Ordering;
838
838
fn main() {
839
839
println!("Guess the number!");
840
840
841
-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
841
+
let secret_number = (rand::random::<u32>() % 100) + 1;
842
842
843
843
loop {
844
844
@@ -847,7 +847,7 @@ fn main() {
847
847
let input = io::stdin().read_line()
848
848
.ok()
849
849
.expect("Failed to read line");
850
-
let input_num: Option<uint> = input.trim().parse();
850
+
let input_num: Option<u32> = input.trim().parse();
0 commit comments