Skip to content

Replace uint with u32 in trpl/guessing-game.md #20892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions src/doc/trpl/guessing-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ use std::rand;
fn main() {
println!("Guess the number!");

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

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

Expand Down Expand Up @@ -283,7 +283,7 @@ use std::cmp::Ordering;
fn main() {
println!("Guess the number!");

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

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

Expand Down Expand Up @@ -318,7 +318,7 @@ $ cargo build
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)
src/main.rs:20 match cmp(input, secret_number) {
^~~~~
src/main.rs:20:22: 20:35 error: mismatched types: expected `i32` but found `uint` (expected i32 but found uint)
src/main.rs:20:22: 20:35 error: mismatched types: expected `i32` but found `u32` (expected i32 but found u32)
src/main.rs:20 match cmp(input, secret_number) {
^~~~~~~~~~~~~
error: aborting due to 2 previous errors
Expand All @@ -328,7 +328,7 @@ This often happens when writing Rust programs, and is one of Rust's greatest
strengths. You try out some code, see if it compiles, and Rust tells you that
you've done something wrong. In this case, our `cmp` function works on integers,
but we've given it unsigned integers. In this case, the fix is easy, because
we wrote the `cmp` function! Let's change it to take `uint`s:
we wrote the `cmp` function! Let's change it to take `u32`s:

```{rust,ignore}
use std::io;
Expand All @@ -338,7 +338,7 @@ use std::cmp::Ordering;
fn main() {
println!("Guess the number!");

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

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

Expand All @@ -358,7 +358,7 @@ fn main() {
}
}

fn cmp(a: uint, b: uint) -> Ordering {
fn cmp(a: u32, b: u32) -> Ordering {
if a < b { Ordering::Less }
else if a > b { Ordering::Greater }
else { Ordering::Equal }
Expand All @@ -370,13 +370,13 @@ And try compiling again:
```bash
$ cargo build
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
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)
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)
src/main.rs:20 match cmp(input, secret_number) {
^~~~~
error: aborting due to previous error
```

This error is similar to the last one: we expected to get a `uint`, but we got
This error is similar to the last one: we expected to get a `u32`, but we got
a `String` instead! That's because our `input` variable is coming from the
standard input, and you can guess anything. Try it:

Expand All @@ -393,37 +393,37 @@ Oops! Also, you'll note that we just ran our program even though it didn't compi
This works because the older version we did successfully compile was still lying
around. Gotta be careful!

Anyway, we have a `String`, but we need a `uint`. What to do? Well, there's
Anyway, we have a `String`, but we need a `u32`. What to do? Well, there's
a function for that:

```{rust,ignore}
let input = io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<uint> = input.parse();
let input_num: Option<u32> = input.parse();
```

The `parse` function takes in a `&str` value and converts it into something.
We tell it what kind of something with a type hint. Remember our type hint with
`random()`? It looked like this:

```{rust,ignore}
rand::random::<uint>();
rand::random::<u32>();
```

There's an alternate way of providing a hint too, and that's declaring the type
in a `let`:

```{rust,ignore}
let x: uint = rand::random();
let x: u32 = rand::random();
```

In this case, we say `x` is a `uint` explicitly, so Rust is able to properly
In this case, we say `x` is a `u32` explicitly, so Rust is able to properly
tell `random()` what to generate. In a similar fashion, both of these work:

```{rust,ignore}
let input_num = "5".parse::<uint>(); // input_num: Option<uint>
let input_num: Option<uint> = "5".parse(); // input_num: Option<uint>
let input_num = "5".parse::<u32>(); // input_num: Option<u32>
let input_num: Option<u32> = "5".parse(); // input_num: Option<u32>
```

Anyway, with us now converting our input to a number, our code looks like this:
Expand All @@ -436,7 +436,7 @@ use std::cmp::Ordering;
fn main() {
println!("Guess the number!");

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

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

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

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

Expand All @@ -456,7 +456,7 @@ fn main() {
}
}

fn cmp(a: uint, b: uint) -> Ordering {
fn cmp(a: u32, b: u32) -> Ordering {
if a < b { Ordering::Less }
else if a > b { Ordering::Greater }
else { Ordering::Equal }
Expand All @@ -468,13 +468,13 @@ Let's try it out!
```bash
$ cargo build
Compiling guessing_game v0.0.1 (file:///home/you/projects/guessing_game)
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)
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)
src/main.rs:22 match cmp(input_num, secret_number) {
^~~~~~~~~
error: aborting due to previous error
```

Oh yeah! Our `input_num` has the type `Option<uint>`, rather than `uint`. We
Oh yeah! Our `input_num` has the type `Option<u32>`, rather than `u32`. We
need to unwrap the Option. If you remember from before, `match` is a great way
to do that. Try this code:

Expand All @@ -486,7 +486,7 @@ use std::cmp::Ordering;
fn main() {
println!("Guess the number!");

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

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

Expand All @@ -495,7 +495,7 @@ fn main() {
let input = io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<uint> = input.parse();
let input_num: Option<u32> = input.parse();

let num = match input_num {
Some(num) => num,
Expand All @@ -515,14 +515,14 @@ fn main() {
}
}

fn cmp(a: uint, b: uint) -> Ordering {
fn cmp(a: u32, b: u32) -> Ordering {
if a < b { Ordering::Less }
else if a > b { Ordering::Greater }
else { Ordering::Equal }
}
```

We use a `match` to either give us the `uint` inside of the `Option`, or else
We use a `match` to either give us the `u32` inside of the `Option`, or else
print an error message and return. Let's give this a shot:

```bash
Expand Down Expand Up @@ -553,7 +553,7 @@ use std::cmp::Ordering;
fn main() {
println!("Guess the number!");

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

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

Expand All @@ -562,7 +562,7 @@ fn main() {
let input = io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<uint> = input.trim().parse();
let input_num: Option<u32> = input.trim().parse();

let num = match input_num {
Some(num) => num,
Expand All @@ -582,7 +582,7 @@ fn main() {
}
}

fn cmp(a: uint, b: uint) -> Ordering {
fn cmp(a: u32, b: u32) -> Ordering {
if a < b { Ordering::Less }
else if a > b { Ordering::Greater }
else { Ordering::Equal }
Expand Down Expand Up @@ -627,7 +627,7 @@ use std::cmp::Ordering;
fn main() {
println!("Guess the number!");

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

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

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

let num = match input_num {
Some(num) => num,
Expand All @@ -659,7 +659,7 @@ fn main() {
}
}

fn cmp(a: uint, b: uint) -> Ordering {
fn cmp(a: u32, b: u32) -> Ordering {
if a < b { Ordering::Less }
else if a > b { Ordering::Greater }
else { Ordering::Equal }
Expand Down Expand Up @@ -703,7 +703,7 @@ use std::cmp::Ordering;
fn main() {
println!("Guess the number!");

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

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

Expand All @@ -714,7 +714,7 @@ fn main() {
let input = io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<uint> = input.trim().parse();
let input_num: Option<u32> = input.trim().parse();

let num = match input_num {
Some(num) => num,
Expand All @@ -738,7 +738,7 @@ fn main() {
}
}

fn cmp(a: uint, b: uint) -> Ordering {
fn cmp(a: u32, b: u32) -> Ordering {
if a < b { Ordering::Less }
else if a > b { Ordering::Greater }
else { Ordering::Equal }
Expand All @@ -759,7 +759,7 @@ use std::cmp::Ordering;
fn main() {
println!("Guess the number!");

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

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

Expand All @@ -770,7 +770,7 @@ fn main() {
let input = io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<uint> = input.trim().parse();
let input_num: Option<u32> = input.trim().parse();

let num = match input_num {
Some(num) => num,
Expand All @@ -794,7 +794,7 @@ fn main() {
}
}

fn cmp(a: uint, b: uint) -> Ordering {
fn cmp(a: u32, b: u32) -> Ordering {
if a < b { Ordering::Less }
else if a > b { Ordering::Greater }
else { Ordering::Equal }
Expand Down Expand Up @@ -838,7 +838,7 @@ use std::cmp::Ordering;
fn main() {
println!("Guess the number!");

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

loop {

Expand All @@ -847,7 +847,7 @@ fn main() {
let input = io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<uint> = input.trim().parse();
let input_num: Option<u32> = input.trim().parse();

let num = match input_num {
Some(num) => num,
Expand All @@ -871,7 +871,7 @@ fn main() {
}
}

fn cmp(a: uint, b: uint) -> Ordering {
fn cmp(a: u32, b: u32) -> Ordering {
if a < b { Ordering::Less }
else if a > b { Ordering::Greater }
else { Ordering::Equal }
Expand Down