diff --git a/src/doc/trpl/ffi.md b/src/doc/trpl/ffi.md index a65325af7be3d..c21d6bc34a82d 100644 --- a/src/doc/trpl/ffi.md +++ b/src/doc/trpl/ffi.md @@ -116,11 +116,11 @@ pub fn compress(src: &[u8]) -> Vec { let psrc = src.as_ptr(); let mut dstlen = snappy_max_compressed_length(srclen); - let mut dst = Vec::with_capacity(dstlen as uint); + let mut dst = Vec::with_capacity(dstlen as usize); let pdst = dst.as_mut_ptr(); snappy_compress(psrc, srclen, pdst, &mut dstlen); - dst.set_len(dstlen as uint); + dst.set_len(dstlen as usize); dst } } @@ -148,11 +148,11 @@ pub fn uncompress(src: &[u8]) -> Option> { let mut dstlen: size_t = 0; snappy_uncompressed_length(psrc, srclen, &mut dstlen); - let mut dst = Vec::with_capacity(dstlen as uint); + let mut dst = Vec::with_capacity(dstlen as usize); let pdst = dst.as_mut_ptr(); if snappy_uncompress(psrc, srclen, pdst, &mut dstlen) == 0 { - dst.set_len(dstlen as uint); + dst.set_len(dstlen as usize); Some(dst) } else { None // SNAPPY_INVALID_INPUT @@ -420,7 +420,7 @@ Unsafe functions, on the other hand, advertise it to the world. An unsafe functi this: ~~~~ -unsafe fn kaboom(ptr: *const int) -> int { *ptr } +unsafe fn kaboom(ptr: *const isize) -> isize { *ptr } ~~~~ This function can only be called from an `unsafe` block or another `unsafe` function. @@ -441,7 +441,7 @@ extern { fn main() { println!("You have readline version {} installed.", - rl_readline_version as int); + rl_readline_version as isize); } ~~~ diff --git a/src/doc/trpl/generics.md b/src/doc/trpl/generics.md index 402f36cd74bd9..711b626312642 100644 --- a/src/doc/trpl/generics.md +++ b/src/doc/trpl/generics.md @@ -5,7 +5,7 @@ multiple types of arguments. For example, remember our `OptionalInt` type? ```{rust} enum OptionalInt { - Value(int), + Value(i32), Missing, } ``` @@ -40,26 +40,26 @@ we substitute that type for the same type used in the generic. Here's an example of using `Option`, with some extra type annotations: ```{rust} -let x: Option = Some(5i); +let x: Option = Some(5i32); ``` -In the type declaration, we say `Option`. Note how similar this looks to -`Option`. So, in this particular `Option`, `T` has the value of `int`. On -the right-hand side of the binding, we do make a `Some(T)`, where `T` is `5i`. -Since that's an `int`, the two sides match, and Rust is happy. If they didn't +In the type declaration, we say `Option`. Note how similar this looks to +`Option`. So, in this particular `Option`, `T` has the value of `i32`. On +the right-hand side of the binding, we do make a `Some(T)`, where `T` is `5i32`. +Since that's an `i32`, the two sides match, and Rust is happy. If they didn't match, we'd get an error: ```{rust,ignore} -let x: Option = Some(5i); +let x: Option = Some(5i32); // error: mismatched types: expected `core::option::Option` -// but found `core::option::Option` (expected f64 but found int) +// but found `core::option::Option` (expected f64 but found i32) ``` That doesn't mean we can't make `Option`s that hold an `f64`! They just have to match up: ```{rust} -let x: Option = Some(5i); +let x: Option = Some(5i32); let y: Option = Some(5.0f64); ``` diff --git a/src/doc/trpl/guessing-game.md b/src/doc/trpl/guessing-game.md index 16bd1e347b7c8..47e27249090ea 100644 --- a/src/doc/trpl/guessing-game.md +++ b/src/doc/trpl/guessing-game.md @@ -239,7 +239,7 @@ use std::rand; fn main() { println!("Guess the number!"); - let secret_number = (rand::random::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; 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::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; 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::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; 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 = input.parse(); +let input_num: Option = 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::(); +rand::random::(); ``` 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::(); // input_num: Option -let input_num: Option = "5".parse(); // input_num: Option +let input_num = "5".parse::(); // input_num: Option +let input_num: Option = "5".parse(); // input_num: Option ``` 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::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; 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 = input.parse(); + let input_num: Option = 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` (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` (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`, rather than `uint`. We +Oh yeah! Our `input_num` has the type `Option`, 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::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; 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 = input.parse(); + let input_num: Option = 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::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; 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 = input.trim().parse(); + let input_num: Option = 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::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; 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 = input.trim().parse(); + let input_num: Option = 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::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; 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 = input.trim().parse(); + let input_num: Option = 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::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; 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 = input.trim().parse(); + let input_num: Option = 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::() % 100u) + 1u; + let secret_number = (rand::random::() % 100u32) + 1u32; loop { @@ -847,7 +847,7 @@ fn main() { let input = io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = input.trim().parse(); + let input_num: Option = 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 } diff --git a/src/doc/trpl/iterators.md b/src/doc/trpl/iterators.md index 8312f762c113a..51dfa96273ee3 100644 --- a/src/doc/trpl/iterators.md +++ b/src/doc/trpl/iterators.md @@ -5,7 +5,7 @@ Let's talk about loops. Remember Rust's `for` loop? Here's an example: ```{rust} -for x in range(0i, 10i) { +for x in range(0i32, 10i32) { println!("{}", x); } ``` @@ -17,7 +17,7 @@ call the `.next()` method on repeatedly, and it gives us a sequence of things. Like this: ```{rust} -let mut range = range(0i, 10i); +let mut range = range(0i32, 10i32); loop { match range.next() { @@ -32,8 +32,8 @@ loop { We make a mutable binding to the return value of `range`, which is our iterator. We then `loop`, with an inner `match`. This `match` is used on the result of `range.next()`, which gives us a reference to the next value of the iterator. -`next` returns an `Option`, in this case, which will be `Some(int)` when -we have a value and `None` once we run out. If we get `Some(int)`, we print it +`next` returns an `Option`, in this case, which will be `Some(i32)` when +we have a value and `None` once we run out. If we get `Some(i32)`, we print it out, and if we get `None`, we `break` out of the loop. This code sample is basically the same as our `for` loop version. The `for` @@ -50,7 +50,7 @@ primitive. For example, if you needed to iterate over the contents of a vector, you may be tempted to write this: ```{rust} -let nums = vec![1i, 2i, 3i]; +let nums = vec![1i32, 2, 3]; for i in range(0u, nums.len()) { println!("{}", nums[i]); @@ -62,7 +62,7 @@ vectors returns an iterator which iterates through a reference to each element of the vector in turn. So write this: ```{rust} -let nums = vec![1i, 2i, 3i]; +let nums = vec![1i32, 2, 3]; for num in nums.iter() { println!("{}", num); @@ -79,12 +79,12 @@ very common with iterators: we can ignore unnecessary bounds checks, but still know that we're safe. There's another detail here that's not 100% clear because of how `println!` -works. `num` is actually of type `&int`. That is, it's a reference to an `int`, -not an `int` itself. `println!` handles the dereferencing for us, so we don't +works. `num` is actually of type `&i32`. That is, it's a reference to an `i32`, +not an `i32` itself. `println!` handles the dereferencing for us, so we don't see it. This code works fine too: ```{rust} -let nums = vec![1i, 2i, 3i]; +let nums = vec![1i32, 2, 3]; for num in nums.iter() { println!("{}", *num); @@ -118,7 +118,7 @@ The most common consumer is `collect()`. This code doesn't quite compile, but it shows the intention: ```{rust,ignore} -let one_to_one_hundred = range(1i, 101i).collect(); +let one_to_one_hundred = range(1i32, 101i32).collect(); ``` As you can see, we call `collect()` on our iterator. `collect()` takes @@ -128,7 +128,7 @@ type of things you want to collect, and so you need to let it know. Here's the version that does compile: ```{rust} -let one_to_one_hundred = range(1i, 101i).collect::>(); +let one_to_one_hundred = range(1i32, 101i32).collect::>(); ``` If you remember, the `::<>` syntax allows us to give a type hint, @@ -138,7 +138,7 @@ and so we tell it that we want a vector of integers. is one: ```{rust} -let greater_than_forty_two = range(0i, 100i) +let greater_than_forty_two = range(0i32, 100) .find(|x| *x > 42); match greater_than_forty_two { @@ -155,8 +155,8 @@ element, `find` returns an `Option` rather than the element itself. Another important consumer is `fold`. Here's what it looks like: ```{rust} -let sum = range(1i, 4i) - .fold(0i, |sum, x| sum + x); +let sum = range(1i32, 4) + .fold(0i32, |sum, x| sum + x); ``` `fold()` is a consumer that looks like this: @@ -172,24 +172,24 @@ in this iterator: | base | accumulator | element | closure result | |------|-------------|---------|----------------| -| 0i | 0i | 1i | 1i | -| 0i | 1i | 2i | 3i | -| 0i | 3i | 3i | 6i | +| 0 | 0 | 1 | 1 | +| 0 | 1 | 2 | 3 | +| 0 | 3 | 3 | 6 | We called `fold()` with these arguments: ```{rust} -# range(1i, 4i) -.fold(0i, |sum, x| sum + x); +# range(1i32, 4) +.fold(0i32, |sum, x| sum + x); ``` -So, `0i` is our base, `sum` is our accumulator, and `x` is our element. On the -first iteration, we set `sum` to `0i`, and `x` is the first element of `nums`, -`1i`. We then add `sum` and `x`, which gives us `0i + 1i = 1i`. On the second +So, `0` is our base, `sum` is our accumulator, and `x` is our element. On the +first iteration, we set `sum` to `0`, and `x` is the first element of `nums`, +`1`. We then add `sum` and `x`, which gives us `0 + 1 = 1`. On the second iteration, that value becomes our accumulator, `sum`, and the element is -the second element of the array, `2i`. `1i + 2i = 3i`, and so that becomes +the second element of the array, `2`. `1 + 2 = 3`, and so that becomes the value of the accumulator for the last iteration. On that iteration, -`x` is the last element, `3i`, and `3i + 3i = 6i`, which is our final +`x` is the last element, `3`, and `3 + 3 = 6`, which is our final result for our sum. `1 + 2 + 3 = 6`, and that's the result we got. Whew. `fold` can be a bit strange the first few times you see it, but once it @@ -210,14 +210,14 @@ This code, for example, does not actually generate the numbers `1-100`, and just creates a value that represents the sequence: ```{rust} -let nums = range(1i, 100i); +let nums = range(1i32, 100i32); ``` Since we didn't do anything with the range, it didn't generate the sequence. Let's add the consumer: ```{rust} -let nums = range(1i, 100i).collect::>(); +let nums = range(1i32, 100i32).collect::>(); ``` Now, `collect()` will require that `range()` give it some numbers, and so @@ -228,7 +228,7 @@ which you've used before. `iter()` can turn a vector into a simple iterator that gives you each element in turn: ```{rust} -let nums = [1i, 2i, 3i]; +let nums = [1i32, 2i32, 3i32]; for num in nums.iter() { println!("{}", num); @@ -239,12 +239,12 @@ These two basic iterators should serve you well. There are some more advanced iterators, including ones that are infinite. Like `count`: ```{rust} -std::iter::count(1i, 5i); +std::iter::count(1i32, 5i32); ``` This iterator counts up from one, adding five each time. It will give you a new integer every time, forever (well, technically, until it reaches the -maximum number representable by an `int`). But since iterators are lazy, +maximum number representable by an `i32`). But since iterators are lazy, that's okay! You probably don't want to use `collect()` on it, though... That's enough about iterators. Iterator adapters are the last concept @@ -256,7 +256,7 @@ we need to talk about with regards to iterators. Let's get to it! a new iterator. The simplest one is called `map`: ```{rust,ignore} -range(1i, 100i).map(|x| x + 1i); +range(1i32, 100).map(|x| x + 1i32); ``` `map` is called upon another iterator, and produces a new iterator where each @@ -267,7 +267,7 @@ compile the example, you'll get a warning: ```{notrust,ignore} warning: unused result which must be used: iterator adaptors are lazy and do nothing unless consumed, #[warn(unused_must_use)] on by default - range(1i, 100i).map(|x| x + 1i); + range(1i32, 100).map(|x| x + 1i32); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` @@ -275,7 +275,7 @@ Laziness strikes again! That closure will never execute. This example doesn't print any numbers: ```{rust,ignore} -range(1i, 100i).map(|x| println!("{}", x)); +range(1i32, 100).map(|x| println!("{}", x)); ``` If you are trying to execute a closure on an iterator for its side effects, @@ -287,7 +287,7 @@ has no side effect on the original iterator. Let's try it out with our infinite iterator from before, `count()`: ```{rust} -for i in std::iter::count(1i, 5i).take(5) { +for i in std::iter::count(1i32, 5).take(5) { println!("{}", i); } ``` @@ -307,7 +307,7 @@ returns `true` or `false`. The new iterator `filter()` produces only the elements that that closure returns `true` for: ```{rust} -for i in range(1i, 100i).filter(|&x| x % 2 == 0) { +for i in range(1i32, 100).filter(|&x| x % 2 == 0) { println!("{}", i); } ``` @@ -322,11 +322,11 @@ You can chain all three things together: start with an iterator, adapt it a few times, and then consume the result. Check it out: ```{rust} -range(1i, 1000i) +range(1i32, 1000i32) .filter(|&x| x % 2 == 0) .filter(|&x| x % 3 == 0) .take(5) - .collect::>(); + .collect::>(); ``` This will give you a vector containing `6`, `12`, `18`, `24`, and `30`. diff --git a/src/doc/trpl/macros.md b/src/doc/trpl/macros.md index c73fbefb2a401..858d8ec3c4e4e 100644 --- a/src/doc/trpl/macros.md +++ b/src/doc/trpl/macros.md @@ -11,8 +11,8 @@ which both pattern-match on their input and both return early in one case, doing nothing otherwise: ~~~~ -# enum T { SpecialA(uint), SpecialB(uint) } -# fn f() -> uint { +# enum T { SpecialA(u32), SpecialB(u32) } +# fn f() -> u32 { # let input_1 = T::SpecialA(0); # let input_2 = T::SpecialA(0); match input_1 { @@ -24,7 +24,7 @@ match input_2 { T::SpecialB(x) => { return x; } _ => {} } -# return 0u; +# return 0u32; # } ~~~~ @@ -37,8 +37,8 @@ lightweight custom syntax extensions, themselves defined using the the pattern in the above code: ~~~~ -# enum T { SpecialA(uint), SpecialB(uint) } -# fn f() -> uint { +# enum T { SpecialA(u32), SpecialB(u32) } +# fn f() -> u32 { # let input_1 = T::SpecialA(0); # let input_2 = T::SpecialA(0); macro_rules! early_return { @@ -86,7 +86,7 @@ To take a fragment of Rust code as an argument, write `$` followed by a name `foo`.) * `expr` (an expression. Examples: `2 + 2`; `if true then { 1 } else { 2 }`; `f(42)`.) -* `ty` (a type. Examples: `int`, `Vec<(char, String)>`, `&T`.) +* `ty` (a type. Examples: `isize`, `Vec<(char, String)>`, `&T`.) * `pat` (a pattern, usually appearing in a `match` or on the left-hand side of a declaration. Examples: `Some(t)`; `(17, 'a')`; `_`.) * `block` (a sequence of actions. Example: `{ log(error, "hi"); return 12; }`) @@ -156,8 +156,8 @@ separator token (a comma-separated list could be written `$(...),*`), and `+` instead of `*` to mean "at least one". ~~~~ -# enum T { SpecialA(uint),SpecialB(uint),SpecialC(uint),SpecialD(uint)} -# fn f() -> uint { +# enum T { SpecialA(u32),SpecialB(u32),SpecialC(u32),SpecialD(u32)} +# fn f() -> u32 { # let input_1 = T::SpecialA(0); # let input_2 = T::SpecialA(0); macro_rules! early_return { @@ -217,10 +217,10 @@ solves the problem. Now consider code like the following: ~~~~ -# enum T1 { Good1(T2, uint), Bad1} +# enum T1 { Good1(T2, u32), Bad1} # struct T2 { body: T3 } -# enum T3 { Good2(uint), Bad2} -# fn f(x: T1) -> uint { +# enum T3 { Good2(u32), Bad2} +# fn f(x: T1) -> u32 { match x { T1::Good1(g1, val) => { match g1.body { @@ -264,10 +264,10 @@ macro_rules! biased_match { ) } -# enum T1 { Good1(T2, uint), Bad1} +# enum T1 { Good1(T2, u32), Bad1} # struct T2 { body: T3 } -# enum T3 { Good2(uint), Bad2} -# fn f(x: T1) -> uint { +# enum T3 { Good2(u32), Bad2} +# fn f(x: T1) -> u32 { biased_match!((x) -> (T1::Good1(g1, val)) else { return 0 }; binds g1, val ); biased_match!((g1.body) -> (T3::Good2(result) ) @@ -374,10 +374,10 @@ macro_rules! biased_match { } -# enum T1 { Good1(T2, uint), Bad1} +# enum T1 { Good1(T2, u32), Bad1} # struct T2 { body: T3 } -# enum T3 { Good2(uint), Bad2} -# fn f(x: T1) -> uint { +# enum T3 { Good2(u32), Bad2} +# fn f(x: T1) -> u32 { biased_match!( (x) -> (T1::Good1(g1, val)) else { return 0 }; (g1.body) -> (T3::Good2(result) ) else { panic!("Didn't get Good2") }; @@ -519,7 +519,7 @@ A further difficulty occurs when a macro is used in multiple crates. Say that `mylib` defines ```rust -pub fn increment(x: uint) -> uint { +pub fn increment(x: u32) -> u32 { x + 1 } diff --git a/src/doc/trpl/patterns.md b/src/doc/trpl/patterns.md index c54d502b4edde..6fdf5137187a4 100644 --- a/src/doc/trpl/patterns.md +++ b/src/doc/trpl/patterns.md @@ -8,7 +8,7 @@ A quick refresher: you can match against literals directly, and `_` acts as an *any* case: ```{rust} -let x = 1i; +let x = 1is; match x { 1 => println!("one"), @@ -47,7 +47,7 @@ If you're matching multiple things, via a `|` or a `...`, you can bind the value to a name with `@`: ```{rust} -let x = 1i; +let x = 1i32; match x { e @ 1 ... 5 => println!("got a range element {}", e), @@ -60,14 +60,14 @@ ignore the value and type in the variant: ```{rust} enum OptionalInt { - Value(int), + Value(i32), Missing, } -let x = OptionalInt::Value(5i); +let x = OptionalInt::Value(5i32); match x { - OptionalInt::Value(..) => println!("Got an int!"), + OptionalInt::Value(..) => println!("Got an integer!"), OptionalInt::Missing => println!("No such luck."), } ``` @@ -76,15 +76,15 @@ You can introduce *match guards* with `if`: ```{rust} enum OptionalInt { - Value(int), + Value(i32), Missing, } -let x = OptionalInt::Value(5i); +let x = OptionalInt::Value(5i32); match x { - OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"), - OptionalInt::Value(..) => println!("Got an int!"), + OptionalInt::Value(i) if i > 5 => println!("Got an integer bigger than five!"), + OptionalInt::Value(..) => println!("Got an integer!"), OptionalInt::Missing => println!("No such luck."), } ``` @@ -93,33 +93,33 @@ If you're matching on a pointer, you can use the same syntax as you declared it with. First, `&`: ```{rust} -let x = &5i; +let x = &5i32; match x { &val => println!("Got a value: {}", val), } ``` -Here, the `val` inside the `match` has type `int`. In other words, the left-hand -side of the pattern destructures the value. If we have `&5i`, then in `&val`, `val` -would be `5i`. +Here, the `val` inside the `match` has type `i32`. In other words, the left-hand +side of the pattern destructures the value. If we have `&5i32`, then in `&val`, `val` +would be `5i32`. If you want to get a reference, use the `ref` keyword: ```{rust} -let x = 5i; +let x = 5i32; match x { ref r => println!("Got a reference to {}", r), } ``` -Here, the `r` inside the `match` has the type `&int`. In other words, the `ref` +Here, the `r` inside the `match` has the type `&i32`. In other words, the `ref` keyword _creates_ a reference, for use in the pattern. If you need a mutable reference, `ref mut` will work in the same way: ```{rust} -let mut x = 5i; +let mut x = 5i32; match x { ref mut mr => println!("Got a mutable reference to {}", mr), @@ -131,11 +131,11 @@ If you have a struct, you can destructure it inside of a pattern: ```{rust} # #![allow(non_shorthand_field_patterns)] struct Point { - x: int, - y: int, + x: i32, + y: i32, } -let origin = Point { x: 0i, y: 0i }; +let origin = Point { x: 0i32, y: 0i32 }; match origin { Point { x: x, y: y } => println!("({},{})", x, y), @@ -147,11 +147,11 @@ If we only care about some of the values, we don't have to give them all names: ```{rust} # #![allow(non_shorthand_field_patterns)] struct Point { - x: int, - y: int, + x: i32, + y: i32, } -let origin = Point { x: 0i, y: 0i }; +let origin = Point { x: 0i32, y: 0i32 }; match origin { Point { x: x, .. } => println!("x is {}", x), @@ -163,11 +163,11 @@ You can do this kind of match on any member, not just the first: ```{rust} # #![allow(non_shorthand_field_patterns)] struct Point { - x: int, - y: int, + x: i32, + y: i32, } -let origin = Point { x: 0i, y: 0i }; +let origin = Point { x: 0i32, y: 0i32 }; match origin { Point { y: y, .. } => println!("y is {}", y), diff --git a/src/doc/trpl/plugins.md b/src/doc/trpl/plugins.md index 2fc361ca1b284..6f6d637da03b3 100644 --- a/src/doc/trpl/plugins.md +++ b/src/doc/trpl/plugins.md @@ -68,7 +68,7 @@ use rustc::plugin::Registry; fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) -> Box { - static NUMERALS: &'static [(&'static str, uint)] = &[ + static NUMERALS: &'static [(&'static str, u32)] = &[ ("M", 1000), ("CM", 900), ("D", 500), ("CD", 400), ("C", 100), ("XC", 90), ("L", 50), ("XL", 40), ("X", 10), ("IX", 9), ("V", 5), ("IV", 4), @@ -118,7 +118,7 @@ fn main() { } ``` -The advantages over a simple `fn(&str) -> uint` are: +The advantages over a simple `fn(&str) -> u32` are: * The (arbitrarily complex) conversion is done at compile time. * Input validation is also performed at compile time. diff --git a/src/doc/trpl/pointers.md b/src/doc/trpl/pointers.md index 63c16ef191e06..1304c6e974de0 100644 --- a/src/doc/trpl/pointers.md +++ b/src/doc/trpl/pointers.md @@ -28,8 +28,8 @@ question](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack- as the rest of this guide assumes you know the difference.) Like this: ```{rust} -let x = 5i; -let y = 8i; +let x = 5i32; +let y = 8i32; ``` | location | value | |----------|-------| @@ -46,8 +46,8 @@ Let's introduce a pointer. In some languages, there is just one type of *reference*, which is the simplest kind of pointer. ```{rust} -let x = 5i; -let y = 8i; +let x = 5i32; +let y = 8i32; let z = &y; ``` |location | value | @@ -58,12 +58,12 @@ let z = &y; See the difference? Rather than contain a value, the value of a pointer is a location in memory. In this case, the location of `y`. `x` and `y` have the -type `int`, but `z` has the type `&int`. We can print this location using the +type `i32`, but `z` has the type `&i32`. We can print this location using the `{:p}` format string: ```{rust} -let x = 5i; -let y = 8i; +let x = 5i32; +let y = 8i32; let z = &y; println!("{:p}", z); @@ -71,12 +71,12 @@ println!("{:p}", z); This would print `0xd3e028`, with our fictional memory addresses. -Because `int` and `&int` are different types, we can't, for example, add them +Because `i32` and `&i32` are different types, we can't, for example, add them together: ```{rust,ignore} -let x = 5i; -let y = 8i; +let x = 5i32; +let y = 8i32; let z = &y; println!("{}", x + z); @@ -85,7 +85,7 @@ println!("{}", x + z); This gives us an error: ```text -hello.rs:6:24: 6:25 error: mismatched types: expected `int` but found `&int` (expected int but found &-ptr) +hello.rs:6:24: 6:25 error: mismatched types: expected `i32` but found `&i32` (expected i32 but found &-ptr) hello.rs:6 println!("{}", x + z); ^ ``` @@ -95,8 +95,8 @@ pointer means accessing the value at the location stored in the pointer. This will work: ```{rust} -let x = 5i; -let y = 8i; +let x = 5i32; +let y = 8i32; let z = &y; println!("{}", x + *z); @@ -252,7 +252,7 @@ The most basic type of pointer that Rust has is called a *reference*. Rust references look like this: ```{rust} -let x = 5i; +let x = 5i32; let y = &x; println!("{}", *y); @@ -269,18 +269,18 @@ referent, because `println!` will automatically dereference it for us. Here's a function that takes a reference: ```{rust} -fn succ(x: &int) -> int { *x + 1 } +fn succ(x: &i32) -> i32 { *x + 1 } ``` You can also use `&` as an operator to create a reference, so we can call this function in two different ways: ```{rust} -fn succ(x: &int) -> int { *x + 1 } +fn succ(x: &i32) -> i32 { *x + 1 } fn main() { - let x = 5i; + let x = 5i32; let y = &x; println!("{}", succ(y)); @@ -294,13 +294,13 @@ Of course, if this were real code, we wouldn't bother with the reference, and just write: ```{rust} -fn succ(x: int) -> int { x + 1 } +fn succ(x: i32) -> i32 { x + 1 } ``` References are immutable by default: ```{rust,ignore} -let x = 5i; +let x = 5i32; let y = &x; *y = 5; // error: cannot assign to immutable dereference of `&`-pointer `*y` @@ -310,21 +310,21 @@ They can be made mutable with `mut`, but only if its referent is also mutable. This works: ```{rust} -let mut x = 5i; +let mut x = 5i32; let y = &mut x; ``` This does not: ```{rust,ignore} -let x = 5i; +let x = 5i32; let y = &mut x; // error: cannot borrow immutable local variable `x` as mutable ``` Immutable pointers are allowed to alias: ```{rust} -let x = 5i; +let x = 5i32; let y = &x; let z = &x; ``` @@ -332,7 +332,7 @@ let z = &x; Mutable ones, however, are not: ```{rust,ignore} -let mut x = 5i; +let mut x = 5i32; let y = &mut x; let z = &mut x; // error: cannot borrow `x` as mutable more than once at a time ``` @@ -348,7 +348,7 @@ Here's the simple explanation: would you expect this code to compile? ```{rust,ignore} fn main() { println!("{}", x); - let x = 5; + let x = 5i32; } ``` @@ -359,7 +359,7 @@ duration a *lifetime*. Let's try a more complex example: ```{rust} fn main() { - let x = &mut 5i; + let x = &mut 5i32; if *x < 10 { let y = &x; @@ -380,7 +380,7 @@ mutated, and therefore, lets us pass. This wouldn't work: ```{rust,ignore} fn main() { - let x = &mut 5i; + let x = &mut 5i32; if *x < 10 { let y = &x; @@ -425,13 +425,13 @@ References just borrow ownership, which is more polite if you don't need the ownership. In other words, prefer: ```{rust} -fn succ(x: &int) -> int { *x + 1 } +fn succ(x: &i32) -> i32 { *x + 1 } ``` to ```{rust} -fn succ(x: Box) -> int { *x + 1 } +fn succ(x: Box) -> i32 { *x + 1 } ``` As a corollary to that rule, references allow you to accept a wide variety of @@ -439,7 +439,7 @@ other pointers, and so are useful so that you don't have to write a number of variants per pointer. In other words, prefer: ```{rust} -fn succ(x: &int) -> int { *x + 1 } +fn succ(x: &i32) -> i32 { *x + 1 } ``` to @@ -447,9 +447,9 @@ to ```{rust} use std::rc::Rc; -fn box_succ(x: Box) -> int { *x + 1 } +fn box_succ(x: Box) -> i32 { *x + 1 } -fn rc_succ(x: Rc) -> int { *x + 1 } +fn rc_succ(x: Rc) -> i32 { *x + 1 } ``` Note that the caller of your function will have to modify their calls slightly: @@ -458,11 +458,11 @@ Note that the caller of your function will have to modify their calls slightly: # use std::boxed::Box; use std::rc::Rc; -fn succ(x: &int) -> int { *x + 1 } +fn succ(x: &i32) -> i32 { *x + 1 } -let ref_x = &5i; -let box_x = Box::new(5i); -let rc_x = Rc::new(5i); +let ref_x = &5i32; +let box_x = Box::new(5i32); +let rc_x = Rc::new(5i32); succ(ref_x); succ(&*box_x); @@ -479,7 +479,7 @@ heap allocation in Rust. Creating a box looks like this: ```{rust} # use std::boxed::Box; -let x = Box::new(5i); +let x = Box::new(5i32); ``` Boxes are heap allocated and they are deallocated automatically by Rust when @@ -488,7 +488,7 @@ they go out of scope: ```{rust} # use std::boxed::Box; { - let x = Box::new(5i); + let x = Box::new(5i32); // stuff happens @@ -509,7 +509,7 @@ boxes, though. As a rough approximation, you can treat this Rust code: ```{rust} # use std::boxed::Box; { - let x = Box::new(5i); + let x = Box::new(5i32); // stuff happens } @@ -549,12 +549,12 @@ Using boxes and references together is very common. For example: ```{rust} # use std::boxed::Box; -fn add_one(x: &int) -> int { +fn add_one(x: &i32) -> i32 { *x + 1 } fn main() { - let x = Box::new(5i); + let x = Box::new(5i32); println!("{}", add_one(&*x)); } @@ -567,12 +567,12 @@ We can borrow `x` multiple times, as long as it's not simultaneous: ```{rust} # use std::boxed::Box; -fn add_one(x: &int) -> int { +fn add_one(x: &i32) -> i32 { *x + 1 } fn main() { - let x = Box::new(5i); + let x = Box::new(5i32); println!("{}", add_one(&*x)); println!("{}", add_one(&*x)); @@ -584,12 +584,12 @@ Or as long as it's not a mutable borrow. This will error: ```{rust,ignore} # use std::boxed::Box; -fn add_one(x: &mut int) -> int { +fn add_one(x: &mut i32) -> i32 { *x + 1 } fn main() { - let x = Box::new(5i); + let x = Box::new(5i32); println!("{}", add_one(&*x)); // error: cannot borrow immutable dereference // of `&`-pointer as mutable @@ -618,7 +618,7 @@ enum List { } fn main() { - let list: List = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Cons(3, Box::new(List::Nil)))))); + let list: List = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Cons(3, Box::new(List::Nil)))))); println!("{:?}", list); } ``` @@ -668,10 +668,10 @@ so as to avoid copying a large data structure. For example: ```{rust} # use std::boxed::Box; struct BigStruct { - one: int, - two: int, + one: i64, + two: i64, // etc - one_hundred: int, + one_hundred: i64, } fn foo(x: Box) -> Box { @@ -680,9 +680,9 @@ fn foo(x: Box) -> Box { fn main() { let x = Box::new(BigStruct { - one: 1, - two: 2, - one_hundred: 100, + one: 1i64, + two: 2i64, + one_hundred: 100i64, }); let y = foo(x); @@ -690,17 +690,17 @@ fn main() { ``` The idea is that by passing around a box, you're only copying a pointer, rather -than the hundred `int`s that make up the `BigStruct`. +than the hundred `i64`s that make up the `BigStruct`. This is an antipattern in Rust. Instead, write this: ```{rust} # use std::boxed::Box; struct BigStruct { - one: int, - two: int, + one: i64, + two: i64, // etc - one_hundred: int, + one_hundred: i64, } fn foo(x: Box) -> BigStruct { @@ -709,9 +709,9 @@ fn foo(x: Box) -> BigStruct { fn main() { let x = Box::new(BigStruct { - one: 1, - two: 2, - one_hundred: 100, + one: 1i64, + two: 2i64, + one_hundred: 100i64, }); let y = Box::new(foo(x)); diff --git a/src/doc/trpl/tasks.md b/src/doc/trpl/tasks.md index 4c6a7f1323fb1..c5407e596064b 100644 --- a/src/doc/trpl/tasks.md +++ b/src/doc/trpl/tasks.md @@ -53,7 +53,7 @@ associated state into an entirely different thread for execution. ```{rust,ignore} # use std::thread::spawn; -# fn generate_thread_number() -> int { 0 } +# fn generate_thread_number() -> u32 { 0 } // Generate some state locally let child_thread_number = generate_thread_number(); @@ -80,7 +80,7 @@ example of calculating two results concurrently: ```{rust,ignore} # use std::thread::spawn; -let (tx, rx): (Sender, Receiver) = channel(); +let (tx, rx): (Sender, Receiver) = channel(); spawn(move || { let result = some_expensive_computation(); @@ -89,7 +89,7 @@ spawn(move || { some_other_expensive_computation(); let result = rx.recv(); -# fn some_expensive_computation() -> int { 42 } +# fn some_expensive_computation() -> i32 { 42 } # fn some_other_expensive_computation() {} ``` @@ -99,7 +99,7 @@ stream for sending and receiving integers (the left-hand side of the `let`, into its component parts). ```{rust,ignore} -let (tx, rx): (Sender, Receiver) = channel(); +let (tx, rx): (Sender, Receiver) = channel(); ``` The child thread will use the sender to send data to the parent thread, which will @@ -108,7 +108,7 @@ thread. ```{rust,ignore} # use std::thread::spawn; -# fn some_expensive_computation() -> int { 42 } +# fn some_expensive_computation() -> i32 { 42 } # let (tx, rx) = channel(); spawn(move || { let result = some_expensive_computation(); @@ -127,7 +127,7 @@ for the child's result to arrive on the receiver: ```{rust,ignore} # fn some_other_expensive_computation() {} -# let (tx, rx) = channel::(); +# let (tx, rx) = channel::(); # tx.send(0); some_other_expensive_computation(); let result = rx.recv(); @@ -140,7 +140,7 @@ single `Receiver` value. What if our example needed to compute multiple results across a number of threads? The following program is ill-typed: ```{rust,ignore} -# fn some_expensive_computation() -> int { 42 } +# fn some_expensive_computation() -> i32 { 42 } let (tx, rx) = channel(); spawn(move || { @@ -168,7 +168,7 @@ for init_val in range(0u, 3) { } let result = rx.recv() + rx.recv() + rx.recv(); -# fn some_expensive_computation(_i: uint) -> int { 42 } +# fn some_expensive_computation(_i: u32) -> i32 { 42 } ``` Cloning a `Sender` produces a new handle to the same channel, allowing multiple @@ -195,7 +195,7 @@ let rxs = Vec::from_fn(3, |init_val| { // Wait on each port, accumulating the results let result = rxs.iter().fold(0, |accum, rx| accum + rx.recv() ); -# fn some_expensive_computation(_i: uint) -> int { 42 } +# fn some_expensive_computation(_i: u32) -> i32 { 42 } ``` ## Backgrounding computations: Futures @@ -212,7 +212,7 @@ use std::sync::Future; # fn main() { # fn make_a_sandwich() {}; fn fib(n: u64) -> u64 { - // lengthy computation returning an uint + // lengthy computation returning an u32 12586269025 } @@ -237,7 +237,7 @@ computations. The workload will be distributed on the available cores. # #![allow(deprecated)] # use std::num::Float; # use std::sync::Future; -fn partial_sum(start: uint) -> f64 { +fn partial_sum(start: u32) -> f64 { let mut local_sum = 0f64; for num in range(start*100000, (start+1)*100000) { local_sum += (num as f64 + 1.0).powf(-2.0); @@ -277,7 +277,7 @@ use std::num::Float; use std::rand; use std::sync::Arc; -fn pnorm(nums: &[f64], p: uint) -> f64 { +fn pnorm(nums: &[f64], p: u32) -> f64 { nums.iter().fold(0.0, |a, b| a + b.powf(p as f64)).powf(1.0 / (p as f64)) } @@ -285,7 +285,7 @@ fn main() { let numbers = Vec::from_fn(1000000, |_| rand::random::()); let numbers_arc = Arc::new(numbers); - for num in range(1u, 10) { + for num in range(1u32, 10) { let thread_numbers = numbers_arc.clone(); spawn(move || { @@ -316,11 +316,11 @@ if it were local. ```{rust,ignore} # use std::rand; # use std::sync::Arc; -# fn pnorm(nums: &[f64], p: uint) -> f64 { 4.0 } +# fn pnorm(nums: &[f64], p: u32) -> f64 { 4.0 } # fn main() { # let numbers=Vec::from_fn(1000000, |_| rand::random::()); # let numbers_arc = Arc::new(numbers); -# let num = 4; +# let num = 4u32; let thread_numbers = numbers_arc.clone(); spawn(move || { // Capture thread_numbers and use it as if it was the underlying vector @@ -345,16 +345,16 @@ each other if they panic. The simplest way of handling a panic is with the `try` function, which is similar to `spawn`, but immediately blocks and waits for the child thread to finish. `try` returns a value of type `Result>`. `Result` is an `enum` type with two variants: -`Ok` and `Err`. In this case, because the type arguments to `Result` are `int` +`Ok` and `Err`. In this case, because the type arguments to `Result` are `i32` and `()`, callers can pattern-match on a result to check whether it's an `Ok` -result with an `int` field (representing a successful result) or an `Err` result +result with an `i32` field (representing a successful result) or an `Err` result (representing termination with an error). ```{rust,ignore} # use std::thread::Thread; # fn some_condition() -> bool { false } -# fn calculate_result() -> int { 0 } -let result: Result> = Thread::spawn(move || { +# fn calculate_result() -> i32 { 0 } +let result: Result> = Thread::spawn(move || { if some_condition() { calculate_result() } else { diff --git a/src/doc/trpl/traits.md b/src/doc/trpl/traits.md index acbcb0b5dd975..2e7f91fe68338 100644 --- a/src/doc/trpl/traits.md +++ b/src/doc/trpl/traits.md @@ -145,25 +145,25 @@ As you can see, `print_area` is now generic, but also ensures that we have passed in the correct types. If we pass in an incorrect type: ```{rust,ignore} -print_area(5i); +print_area(5i64); ``` We get a compile-time error: ```text -error: failed to find an implementation of trait main::HasArea for int +error: failed to find an implementation of trait main::HasArea for i64 ``` So far, we've only added trait implementations to structs, but you can implement a trait for any type. So technically, we _could_ implement -`HasArea` for `int`: +`HasArea` for `i64`: ```{rust} trait HasArea { fn area(&self) -> f64; } -impl HasArea for int { +impl HasArea for i64 { fn area(&self) -> f64 { println!("this is silly"); @@ -171,7 +171,7 @@ impl HasArea for int { } } -5i.area(); +5i64.area(); ``` It is considered poor style to implement methods on such primitive types, even @@ -259,13 +259,13 @@ fn main() { } ``` -This means that even if someone does something bad like add methods to `int`, +This means that even if someone does something bad like add methods to `i64`, it won't affect you, unless you `use` that trait. There's one more restriction on implementing traits. Either the trait or the type you're writing the `impl` for must be inside your crate. So, we could -implement the `HasArea` type for `int`, because `HasArea` is in our crate. But -if we tried to implement `Float`, a trait provided by Rust, for `int`, we could +implement the `HasArea` type for `i64`, because `HasArea` is in our crate. But +if we tried to implement `Float`, a trait provided by Rust, for `i64`, we could not, because both the trait and the type aren't in our crate. One last thing about traits: generic functions with a trait bound use diff --git a/src/doc/trpl/unsafe.md b/src/doc/trpl/unsafe.md index 38427875a6230..e789ac8a38ead 100644 --- a/src/doc/trpl/unsafe.md +++ b/src/doc/trpl/unsafe.md @@ -95,7 +95,7 @@ offered by the Rust language and libraries. For example, they use-after-free; - are considered sendable (if their contents is considered sendable), so the compiler offers no assistance with ensuring their use is - thread-safe; for example, one can concurrently access a `*mut int` + thread-safe; for example, one can concurrently access a `*mut i32` from two threads without synchronization. - lack any form of lifetimes, unlike `&`, and so the compiler cannot reason about dangling pointers; and @@ -368,7 +368,7 @@ expressions must be mutable lvalues: ``` # #![feature(asm)] # #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] -fn add(a: int, b: int) -> int { +fn add(a: isize, b: isize) -> isize { let mut c = 0; unsafe { asm!("add $2, $0" @@ -379,7 +379,7 @@ fn add(a: int, b: int) -> int { c } # #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] -# fn add(a: int, b: int) -> int { a + b } +# fn add(a: isize, b: isize) -> isize { a + b } fn main() { assert_eq!(add(3, 14159), 14162) @@ -455,7 +455,7 @@ extern crate libc; // Entry point for this program #[start] -fn start(_argc: int, _argv: *const *const u8) -> int { +fn start(_argc: isize, _argv: *const *const u8) -> isize { 0 } @@ -481,7 +481,7 @@ compiler's name mangling too: extern crate libc; #[no_mangle] // ensure that this symbol is called `main` in the output -pub extern fn main(argc: int, argv: *const *const u8) -> int { +pub extern fn main(argc: isize, argv: *const *const u8) -> isize { 0 } @@ -554,8 +554,8 @@ pub extern fn dot_product(a: *const u32, a_len: u32, // cannot tell the pointers are valid. let (a_slice, b_slice): (&[u32], &[u32]) = unsafe { mem::transmute(( - Slice { data: a, len: a_len as uint }, - Slice { data: b, len: b_len as uint }, + Slice { data: a, len: a_len as usize }, + Slice { data: b, len: b_len as usize }, )) }; @@ -570,13 +570,13 @@ pub extern fn dot_product(a: *const u32, a_len: u32, #[lang = "panic_fmt"] extern fn panic_fmt(args: &core::fmt::Arguments, file: &str, - line: uint) -> ! { + line: usize) -> ! { loop {} } #[lang = "stack_exhausted"] extern fn stack_exhausted() {} #[lang = "eh_personality"] extern fn eh_personality() {} -# #[start] fn start(argc: int, argv: *const *const u8) -> int { 0 } +# #[start] fn start(argc: isize, argv: *const *const u8) -> isize { 0 } # fn main() {} ``` @@ -630,7 +630,7 @@ via a declaration like extern "rust-intrinsic" { fn transmute(x: T) -> U; - fn offset(dst: *const T, offset: int) -> *const T; + fn offset(dst: *const T, offset: isize) -> *const T; } ``` @@ -667,24 +667,24 @@ extern { pub struct Box(*mut T); #[lang="exchange_malloc"] -unsafe fn allocate(size: uint, _align: uint) -> *mut u8 { +unsafe fn allocate(size: usize, _align: usize) -> *mut u8 { let p = libc::malloc(size as libc::size_t) as *mut u8; // malloc failed - if p as uint == 0 { + if p as usize == 0 { abort(); } p } #[lang="exchange_free"] -unsafe fn deallocate(ptr: *mut u8, _size: uint, _align: uint) { +unsafe fn deallocate(ptr: *mut u8, _size: usize, _align: usize) { libc::free(ptr as *mut libc::c_void) } #[start] -fn main(argc: int, argv: *const *const u8) -> int { - let x = box 1i; +fn main(argc: isize, argv: *const *const u8) -> isize { + let x = box 1is; 0 }