diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-01-13 10:40:18 -0500 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-01-17 10:51:07 -0500 |
| commit | 899ffcf62adde4cef2af0d543fc3fa627396a586 (patch) | |
| tree | a705271031fc4bfb5e3bbccc99c012145088f25b /src/doc/trpl/pointers.md | |
| parent | 078bd498b9fa6eab40df147ce6015ab9aae62b40 (diff) | |
| download | rust-899ffcf62adde4cef2af0d543fc3fa627396a586.tar.gz rust-899ffcf62adde4cef2af0d543fc3fa627396a586.zip | |
Intpocalypse, book edition.
Fix all usage of int/uint/i/u in the book.
Diffstat (limited to 'src/doc/trpl/pointers.md')
| -rw-r--r-- | src/doc/trpl/pointers.md | 104 |
1 files changed, 53 insertions, 51 deletions
diff --git a/src/doc/trpl/pointers.md b/src/doc/trpl/pointers.md index 6832d75245e..c918a80a86f 100644 --- a/src/doc/trpl/pointers.md +++ b/src/doc/trpl/pointers.md @@ -28,9 +28,10 @@ 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 = 5; +let y = 8; ``` + | location | value | |----------|-------| | 0xd3e030 | 5 | @@ -46,10 +47,11 @@ 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 = 5; +let y = 8; let z = &y; ``` + |location | value | |-------- |----------| |0xd3e030 | 5 | @@ -58,12 +60,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 = 5; +let y = 8; let z = &y; println!("{:p}", z); @@ -71,12 +73,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 = 5; +let y = 8; let z = &y; println!("{}", x + z); @@ -85,7 +87,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 +97,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 = 5; +let y = 8; let z = &y; println!("{}", x + *z); @@ -153,7 +155,7 @@ So what do pointers have to do with this? Well, since pointers point to a location in memory... ```text -func foo(&int x) { +func foo(&i32 x) { *x = 5 } @@ -252,7 +254,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 = 5; let y = &x; println!("{}", *y); @@ -269,18 +271,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 = 5; let y = &x; println!("{}", succ(y)); @@ -294,13 +296,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 = 5; let y = &x; *y = 5; // error: cannot assign to immutable dereference of `&`-pointer `*y` @@ -310,21 +312,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 = 5; let y = &mut x; ``` This does not: ```{rust,ignore} -let x = 5i; +let x = 5; 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 = 5; let y = &x; let z = &x; ``` @@ -332,7 +334,7 @@ let z = &x; Mutable ones, however, are not: ```{rust,ignore} -let mut x = 5i; +let mut x = 5; let y = &mut x; let z = &mut x; // error: cannot borrow `x` as mutable more than once at a time ``` @@ -359,7 +361,7 @@ duration a *lifetime*. Let's try a more complex example: ```{rust} fn main() { - let x = &mut 5i; + let x = &mut 5; if *x < 10 { let y = &x; @@ -380,7 +382,7 @@ mutated, and therefore, lets us pass. This wouldn't work: ```{rust,ignore} fn main() { - let x = &mut 5i; + let x = &mut 5; if *x < 10 { let y = &x; @@ -425,13 +427,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>) -> int { *x + 1 } +fn succ(x: Box<i32>) -> i32 { *x + 1 } ``` As a corollary to that rule, references allow you to accept a wide variety of @@ -439,7 +441,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 +449,9 @@ to ```{rust} use std::rc::Rc; -fn box_succ(x: Box<int>) -> int { *x + 1 } +fn box_succ(x: Box<i32>) -> i32 { *x + 1 } -fn rc_succ(x: Rc<int>) -> int { *x + 1 } +fn rc_succ(x: Rc<i32>) -> i32 { *x + 1 } ``` Note that the caller of your function will have to modify their calls slightly: @@ -457,11 +459,11 @@ Note that the caller of your function will have to modify their calls slightly: ```{rust} 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 = &5; +let box_x = Box::new(5); +let rc_x = Rc::new(5); succ(ref_x); succ(&*box_x); @@ -477,7 +479,7 @@ those contents. heap allocation in Rust. Creating a box looks like this: ```{rust} -let x = Box::new(5i); +let x = Box::new(5); ``` Boxes are heap allocated and they are deallocated automatically by Rust when @@ -485,7 +487,7 @@ they go out of scope: ```{rust} { - let x = Box::new(5i); + let x = Box::new(5); // stuff happens @@ -505,7 +507,7 @@ boxes, though. As a rough approximation, you can treat this Rust code: ```{rust} { - let x = Box::new(5i); + let x = Box::new(5); // stuff happens } @@ -544,12 +546,12 @@ for more detail on how lifetimes work. Using boxes and references together is very common. For example: ```{rust} -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(5); println!("{}", add_one(&*x)); } @@ -561,12 +563,12 @@ function, and since it's only reading the value, allows it. We can borrow `x` multiple times, as long as it's not simultaneous: ```{rust} -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(5); println!("{}", add_one(&*x)); println!("{}", add_one(&*x)); @@ -577,12 +579,12 @@ fn main() { Or as long as it's not a mutable borrow. This will error: ```{rust,ignore} -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(5); println!("{}", add_one(&*x)); // error: cannot borrow immutable dereference // of `&`-pointer as mutable @@ -610,7 +612,7 @@ enum List<T> { } fn main() { - let list: List<int> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Cons(3, Box::new(List::Nil)))))); + let list: List<i32> = List::Cons(1, Box::new(List::Cons(2, Box::new(List::Cons(3, Box::new(List::Nil)))))); println!("{:?}", list); } ``` @@ -659,10 +661,10 @@ so as to avoid copying a large data structure. For example: ```{rust} struct BigStruct { - one: int, - two: int, + one: i32, + two: i32, // etc - one_hundred: int, + one_hundred: i32, } fn foo(x: Box<BigStruct>) -> Box<BigStruct> { @@ -687,10 +689,10 @@ This is an antipattern in Rust. Instead, write this: ```{rust} struct BigStruct { - one: int, - two: int, + one: i32, + two: i32, // etc - one_hundred: int, + one_hundred: i32, } fn foo(x: Box<BigStruct>) -> BigStruct { |
