Skip to content

Commit e5963d1

Browse files
committed
Merge pull request #20892 from CarVac/master
Replace uint with u32 in trpl/guessing-game.md Reviewed-by: steveklabnik
2 parents ff4f1f0 + 9302dc5 commit e5963d1

File tree

1 file changed

+39
-39
lines changed

1 file changed

+39
-39
lines changed

src/doc/trpl/guessing-game.md

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ use std::rand;
239239
fn main() {
240240
println!("Guess the number!");
241241
242-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
242+
let secret_number = (rand::random::<u32>() % 100) + 1;
243243
244244
println!("The secret number is: {}", secret_number);
245245
@@ -283,7 +283,7 @@ use std::cmp::Ordering;
283283
fn main() {
284284
println!("Guess the number!");
285285
286-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
286+
let secret_number = (rand::random::<u32>() % 100) + 1;
287287
288288
println!("The secret number is: {}", secret_number);
289289
@@ -318,7 +318,7 @@ $ cargo build
318318
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)
319319
src/main.rs:20 match cmp(input, secret_number) {
320320
^~~~~
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)
322322
src/main.rs:20 match cmp(input, secret_number) {
323323
^~~~~~~~~~~~~
324324
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
328328
strengths. You try out some code, see if it compiles, and Rust tells you that
329329
you've done something wrong. In this case, our `cmp` function works on integers,
330330
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:
332332
333333
```{rust,ignore}
334334
use std::io;
@@ -338,7 +338,7 @@ use std::cmp::Ordering;
338338
fn main() {
339339
println!("Guess the number!");
340340

341-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
341+
let secret_number = (rand::random::<u32>() % 100) + 1;
342342

343343
println!("The secret number is: {}", secret_number);
344344

@@ -358,7 +358,7 @@ fn main() {
358358
}
359359
}
360360

361-
fn cmp(a: uint, b: uint) -> Ordering {
361+
fn cmp(a: u32, b: u32) -> Ordering {
362362
if a < b { Ordering::Less }
363363
else if a > b { Ordering::Greater }
364364
else { Ordering::Equal }
@@ -370,13 +370,13 @@ And try compiling again:
370370
```bash
371371
$ cargo build
372372
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
373-
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)
374374
src/main.rs:20 match cmp(input, secret_number) {
375375
^~~~~
376376
error: aborting due to previous error
377377
```
378378
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
380380
a `String` instead! That's because our `input` variable is coming from the
381381
standard input, and you can guess anything. Try it:
382382
@@ -393,37 +393,37 @@ Oops! Also, you'll note that we just ran our program even though it didn't compi
393393
This works because the older version we did successfully compile was still lying
394394
around. Gotta be careful!
395395
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
397397
a function for that:
398398
399399
```{rust,ignore}
400400
let input = io::stdin().read_line()
401401
.ok()
402402
.expect("Failed to read line");
403-
let input_num: Option<uint> = input.parse();
403+
let input_num: Option<u32> = input.parse();
404404
```
405405
406406
The `parse` function takes in a `&str` value and converts it into something.
407407
We tell it what kind of something with a type hint. Remember our type hint with
408408
`random()`? It looked like this:
409409
410410
```{rust,ignore}
411-
rand::random::<uint>();
411+
rand::random::<u32>();
412412
```
413413
414414
There's an alternate way of providing a hint too, and that's declaring the type
415415
in a `let`:
416416
417417
```{rust,ignore}
418-
let x: uint = rand::random();
418+
let x: u32 = rand::random();
419419
```
420420
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
422422
tell `random()` what to generate. In a similar fashion, both of these work:
423423
424424
```{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>
427427
```
428428
429429
Anyway, with us now converting our input to a number, our code looks like this:
@@ -436,7 +436,7 @@ use std::cmp::Ordering;
436436
fn main() {
437437
println!("Guess the number!");
438438

439-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
439+
let secret_number = (rand::random::<u32>() % 100) + 1;
440440

441441
println!("The secret number is: {}", secret_number);
442442

@@ -445,7 +445,7 @@ fn main() {
445445
let input = io::stdin().read_line()
446446
.ok()
447447
.expect("Failed to read line");
448-
let input_num: Option<uint> = input.parse();
448+
let input_num: Option<u32> = input.parse();
449449

450450
println!("You guessed: {}", input_num);
451451

@@ -456,7 +456,7 @@ fn main() {
456456
}
457457
}
458458

459-
fn cmp(a: uint, b: uint) -> Ordering {
459+
fn cmp(a: u32, b: u32) -> Ordering {
460460
if a < b { Ordering::Less }
461461
else if a > b { Ordering::Greater }
462462
else { Ordering::Equal }
@@ -468,13 +468,13 @@ Let's try it out!
468468
```bash
469469
$ cargo build
470470
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
471-
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)
472472
src/main.rs:22 match cmp(input_num, secret_number) {
473473
^~~~~~~~~
474474
error: aborting due to previous error
475475
```
476476
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
478478
need to unwrap the Option. If you remember from before, `match` is a great way
479479
to do that. Try this code:
480480
@@ -486,7 +486,7 @@ use std::cmp::Ordering;
486486
fn main() {
487487
println!("Guess the number!");
488488
489-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
489+
let secret_number = (rand::random::<u32>() % 100) + 1;
490490
491491
println!("The secret number is: {}", secret_number);
492492
@@ -495,7 +495,7 @@ fn main() {
495495
let input = io::stdin().read_line()
496496
.ok()
497497
.expect("Failed to read line");
498-
let input_num: Option<uint> = input.parse();
498+
let input_num: Option<u32> = input.parse();
499499
500500
let num = match input_num {
501501
Some(num) => num,
@@ -515,14 +515,14 @@ fn main() {
515515
}
516516
}
517517
518-
fn cmp(a: uint, b: uint) -> Ordering {
518+
fn cmp(a: u32, b: u32) -> Ordering {
519519
if a < b { Ordering::Less }
520520
else if a > b { Ordering::Greater }
521521
else { Ordering::Equal }
522522
}
523523
```
524524
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
526526
print an error message and return. Let's give this a shot:
527527
528528
```bash
@@ -553,7 +553,7 @@ use std::cmp::Ordering;
553553
fn main() {
554554
println!("Guess the number!");
555555
556-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
556+
let secret_number = (rand::random::<u32>() % 100) + 1;
557557
558558
println!("The secret number is: {}", secret_number);
559559
@@ -562,7 +562,7 @@ fn main() {
562562
let input = io::stdin().read_line()
563563
.ok()
564564
.expect("Failed to read line");
565-
let input_num: Option<uint> = input.trim().parse();
565+
let input_num: Option<u32> = input.trim().parse();
566566
567567
let num = match input_num {
568568
Some(num) => num,
@@ -582,7 +582,7 @@ fn main() {
582582
}
583583
}
584584
585-
fn cmp(a: uint, b: uint) -> Ordering {
585+
fn cmp(a: u32, b: u32) -> Ordering {
586586
if a < b { Ordering::Less }
587587
else if a > b { Ordering::Greater }
588588
else { Ordering::Equal }
@@ -627,7 +627,7 @@ use std::cmp::Ordering;
627627
fn main() {
628628
println!("Guess the number!");
629629

630-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
630+
let secret_number = (rand::random::<u32>() % 100) + 1;
631631

632632
println!("The secret number is: {}", secret_number);
633633

@@ -638,7 +638,7 @@ fn main() {
638638
let input = io::stdin().read_line()
639639
.ok()
640640
.expect("Failed to read line");
641-
let input_num: Option<uint> = input.trim().parse();
641+
let input_num: Option<u32> = input.trim().parse();
642642

643643
let num = match input_num {
644644
Some(num) => num,
@@ -659,7 +659,7 @@ fn main() {
659659
}
660660
}
661661

662-
fn cmp(a: uint, b: uint) -> Ordering {
662+
fn cmp(a: u32, b: u32) -> Ordering {
663663
if a < b { Ordering::Less }
664664
else if a > b { Ordering::Greater }
665665
else { Ordering::Equal }
@@ -703,7 +703,7 @@ use std::cmp::Ordering;
703703
fn main() {
704704
println!("Guess the number!");
705705
706-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
706+
let secret_number = (rand::random::<u32>() % 100) + 1;
707707
708708
println!("The secret number is: {}", secret_number);
709709
@@ -714,7 +714,7 @@ fn main() {
714714
let input = io::stdin().read_line()
715715
.ok()
716716
.expect("Failed to read line");
717-
let input_num: Option<uint> = input.trim().parse();
717+
let input_num: Option<u32> = input.trim().parse();
718718
719719
let num = match input_num {
720720
Some(num) => num,
@@ -738,7 +738,7 @@ fn main() {
738738
}
739739
}
740740
741-
fn cmp(a: uint, b: uint) -> Ordering {
741+
fn cmp(a: u32, b: u32) -> Ordering {
742742
if a < b { Ordering::Less }
743743
else if a > b { Ordering::Greater }
744744
else { Ordering::Equal }
@@ -759,7 +759,7 @@ use std::cmp::Ordering;
759759
fn main() {
760760
println!("Guess the number!");
761761
762-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
762+
let secret_number = (rand::random::<u32>() % 100) + 1;
763763
764764
println!("The secret number is: {}", secret_number);
765765
@@ -770,7 +770,7 @@ fn main() {
770770
let input = io::stdin().read_line()
771771
.ok()
772772
.expect("Failed to read line");
773-
let input_num: Option<uint> = input.trim().parse();
773+
let input_num: Option<u32> = input.trim().parse();
774774
775775
let num = match input_num {
776776
Some(num) => num,
@@ -794,7 +794,7 @@ fn main() {
794794
}
795795
}
796796
797-
fn cmp(a: uint, b: uint) -> Ordering {
797+
fn cmp(a: u32, b: u32) -> Ordering {
798798
if a < b { Ordering::Less }
799799
else if a > b { Ordering::Greater }
800800
else { Ordering::Equal }
@@ -838,7 +838,7 @@ use std::cmp::Ordering;
838838
fn main() {
839839
println!("Guess the number!");
840840
841-
let secret_number = (rand::random::<uint>() % 100u) + 1u;
841+
let secret_number = (rand::random::<u32>() % 100) + 1;
842842
843843
loop {
844844
@@ -847,7 +847,7 @@ fn main() {
847847
let input = io::stdin().read_line()
848848
.ok()
849849
.expect("Failed to read line");
850-
let input_num: Option<uint> = input.trim().parse();
850+
let input_num: Option<u32> = input.trim().parse();
851851
852852
let num = match input_num {
853853
Some(num) => num,
@@ -871,7 +871,7 @@ fn main() {
871871
}
872872
}
873873
874-
fn cmp(a: uint, b: uint) -> Ordering {
874+
fn cmp(a: u32, b: u32) -> Ordering {
875875
if a < b { Ordering::Less }
876876
else if a > b { Ordering::Greater }
877877
else { Ordering::Equal }

0 commit comments

Comments
 (0)