rollup merge of #20892: CarVac/master

`uint` was recently deprecated, so in following the use of `i32` in the first parts, replace all copies of `uint` with `u32`.
This commit is contained in:
Alex Crichton 2015-01-15 14:11:28 -08:00
commit d3c5cf1b35

View file

@ -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);
@ -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);
@ -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
@ -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;
@ -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);
@ -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 }
@ -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:
@ -393,14 +393,14 @@ 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.
@ -408,22 +408,22 @@ 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:
@ -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);
@ -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);
@ -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 }
@ -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:
@ -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);
@ -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,
@ -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
@ -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);
@ -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,
@ -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 }
@ -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);
@ -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,
@ -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 }
@ -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);
@ -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,
@ -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 }
@ -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);
@ -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,
@ -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 }
@ -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 {
@ -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,
@ -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 }