@@ -83,13 +83,13 @@ Rust:
83
83
``` rust
84
84
# use std :: boxed :: Box ;
85
85
{
86
- let x = Box :: new (5i );
86
+ let x = Box :: new (5 );
87
87
}
88
88
```
89
89
90
- The ` Box::new ` function creates a ` Box<T> ` (specifically ` Box<int > ` in this
90
+ The ` Box::new ` function creates a ` Box<T> ` (specifically ` Box<i32 > ` in this
91
91
case) by allocating a small segment of memory on the heap with enough space to
92
- fit an ` int ` . But where in the code is the box deallocated? We said before that
92
+ fit an ` i32 ` . But where in the code is the box deallocated? We said before that
93
93
we must have a deallocation for each allocation. Rust handles this for you. It
94
94
knows that our handle, ` x ` , is the owning reference to our box. Rust knows that
95
95
` x ` will go out of scope at the end of the block, and so it inserts a call to
@@ -103,12 +103,12 @@ to a function? Let's look at some code:
103
103
``` rust
104
104
# use std :: boxed :: Box ;
105
105
fn main () {
106
- let x = Box :: new (5i );
106
+ let x = Box :: new (5 );
107
107
108
108
add_one (x );
109
109
}
110
110
111
- fn add_one (mut num : Box <int >) {
111
+ fn add_one (mut num : Box <i32 >) {
112
112
* num += 1 ;
113
113
}
114
114
```
@@ -119,14 +119,14 @@ code, where we print out the value of `x`:
119
119
``` {rust,ignore}
120
120
# use std::boxed::Box;
121
121
fn main() {
122
- let x = Box::new(5i );
122
+ let x = Box::new(5 );
123
123
124
124
add_one(x);
125
125
126
126
println!("{}", x);
127
127
}
128
128
129
- fn add_one(mut num: Box<int >) {
129
+ fn add_one(mut num: Box<i32 >) {
130
130
*num += 1;
131
131
}
132
132
```
@@ -153,14 +153,14 @@ box:
153
153
``` rust
154
154
# use std :: boxed :: Box ;
155
155
fn main () {
156
- let x = Box :: new (5i );
156
+ let x = Box :: new (5 );
157
157
158
158
let y = add_one (x );
159
159
160
160
println! (" {}" , y );
161
161
}
162
162
163
- fn add_one (mut num : Box <int >) -> Box <int > {
163
+ fn add_one (mut num : Box <i32 >) -> Box <i32 > {
164
164
* num += 1 ;
165
165
166
166
num
@@ -179,7 +179,7 @@ to something another handle owns. It's called *borrowing*, and it's done with
179
179
Here's the current state of our ` add_one ` function:
180
180
181
181
``` rust
182
- fn add_one (mut num : Box <int >) -> Box <int > {
182
+ fn add_one (mut num : Box <i32 >) -> Box <i32 > {
183
183
* num += 1 ;
184
184
185
185
num
@@ -199,12 +199,12 @@ period. This is also called *borrowing*. Here's a version of `add_one` which
199
199
borrows its argument rather than taking ownership:
200
200
201
201
``` rust
202
- fn add_one (num : & mut int ) {
202
+ fn add_one (num : & mut i32 ) {
203
203
* num += 1 ;
204
204
}
205
205
```
206
206
207
- This function borrows an ` int ` from its caller, and then increments it. When
207
+ This function borrows an ` i32 ` from its caller, and then increments it. When
208
208
the function is over, and ` num ` goes out of scope, the borrow is over.
209
209
210
210
# Lifetimes
@@ -225,10 +225,10 @@ To fix this, we have to make sure that step four never happens after step
225
225
three. The ownership system in Rust does this through a concept called
226
226
* lifetimes* , which describe the scope that a reference is valid for.
227
227
228
- Let's look at that function which borrows an ` int ` again:
228
+ Let's look at that function which borrows an ` i32 ` again:
229
229
230
230
``` rust
231
- fn add_one (num : & int ) -> int {
231
+ fn add_one (num : & i32 ) -> i32 {
232
232
* num + 1
233
233
}
234
234
```
@@ -239,7 +239,7 @@ cover the others later. Without eliding the lifetimes, `add_one` looks like
239
239
this:
240
240
241
241
``` rust
242
- fn add_one <'a >(num : & 'a int ) -> int {
242
+ fn add_one <'a >(num : & 'a i32 ) -> i32 {
243
243
* num + 1
244
244
}
245
245
```
@@ -262,22 +262,22 @@ fn add_two<'a, 'b>(...)
262
262
Then in our parameter list, we use the lifetimes we've named:
263
263
264
264
``` {rust,ignore}
265
- ...(num: &'a int ) -> ...
265
+ ...(num: &'a i32 ) -> ...
266
266
```
267
267
268
- If you compare ` &int ` to ` &'a int ` , they're the same, it's just that the
269
- lifetime ` 'a ` has snuck in between the ` & ` and the ` int ` . We read ` &int ` as "a
270
- reference to an int " and ` &'a int ` as "a reference to an int with the lifetime 'a.'"
268
+ If you compare ` &i32 ` to ` &'a i32 ` , they're the same, it's just that the
269
+ lifetime ` 'a ` has snuck in between the ` & ` and the ` i32 ` . We read ` &i32 ` as "a
270
+ reference to an i32 " and ` &'a i32 ` as "a reference to an i32 with the lifetime 'a.'"
271
271
272
272
Why do lifetimes matter? Well, for example, here's some code:
273
273
274
274
``` rust
275
275
struct Foo <'a > {
276
- x : & 'a int ,
276
+ x : & 'a i32 ,
277
277
}
278
278
279
279
fn main () {
280
- let y = & 5i ; // this is the same as `let _y = 5; let y = &_y;
280
+ let y = & 5 ; // this is the same as `let _y = 5; let y = &_y;
281
281
let f = Foo { x : y };
282
282
283
283
println! (" {}" , f . x);
@@ -288,20 +288,20 @@ As you can see, `struct`s can also have lifetimes. In a similar way to functions
288
288
289
289
``` {rust}
290
290
struct Foo<'a> {
291
- # x: &'a int ,
291
+ # x: &'a i32 ,
292
292
# }
293
293
```
294
294
295
295
declares a lifetime, and
296
296
297
297
``` rust
298
298
# struct Foo <'a > {
299
- x : & 'a int ,
299
+ x : & 'a i32 ,
300
300
# }
301
301
```
302
302
303
303
uses it. So why do we need a lifetime here? We need to ensure that any reference
304
- to a ` Foo ` cannot outlive the reference to an ` int ` it contains.
304
+ to a ` Foo ` cannot outlive the reference to an ` i32 ` it contains.
305
305
306
306
## Thinking in scopes
307
307
@@ -310,7 +310,7 @@ valid for. For example:
310
310
311
311
``` rust
312
312
fn main () {
313
- let y = & 5i ; // -+ y goes into scope
313
+ let y = & 5 ; // -+ y goes into scope
314
314
// |
315
315
// stuff // |
316
316
// |
@@ -321,11 +321,11 @@ Adding in our `Foo`:
321
321
322
322
``` rust
323
323
struct Foo <'a > {
324
- x : & 'a int ,
324
+ x : & 'a i32 ,
325
325
}
326
326
327
327
fn main () {
328
- let y = & 5i ; // -+ y goes into scope
328
+ let y = & 5 ; // -+ y goes into scope
329
329
let f = Foo { x : y }; // -+ f goes into scope
330
330
// stuff // |
331
331
// |
@@ -337,14 +337,14 @@ This code won't work:
337
337
338
338
``` {rust,ignore}
339
339
struct Foo<'a> {
340
- x: &'a int ,
340
+ x: &'a i32 ,
341
341
}
342
342
343
343
fn main() {
344
344
let x; // -+ x goes into scope
345
345
// |
346
346
{ // |
347
- let y = &5i ; // ---+ y goes into scope
347
+ let y = &5 ; // ---+ y goes into scope
348
348
let f = Foo { x: y }; // ---+ f goes into scope
349
349
x = &f.x; // | | error here
350
350
} // ---+ f and y go out of scope
@@ -375,12 +375,12 @@ alive: they are baked into the data segment of the final binary. Another
375
375
example are globals:
376
376
377
377
``` rust
378
- static FOO : int = 5i ;
379
- let x : & 'static int = & FOO ;
378
+ static FOO : i32 = 5 ;
379
+ let x : & 'static i32 = & FOO ;
380
380
```
381
381
382
- This adds an ` int ` to the data segment of the binary, and FOO is a reference to
383
- it.
382
+ This adds an ` i32 ` to the data segment of the binary, and ` FOO ` is a reference
383
+ to it.
384
384
385
385
# Shared Ownership
386
386
@@ -395,7 +395,7 @@ struct Car {
395
395
}
396
396
397
397
struct Wheel {
398
- size: int ,
398
+ size: i32 ,
399
399
owner: Car,
400
400
}
401
401
@@ -431,7 +431,7 @@ struct Car {
431
431
}
432
432
433
433
struct Wheel {
434
- size : int ,
434
+ size : i32 ,
435
435
owner : Rc <Car >,
436
436
}
437
437
@@ -504,15 +504,15 @@ what the elided lifetimes are expand to:
504
504
fn print(s: &str); // elided
505
505
fn print<'a>(s: &'a str); // expanded
506
506
507
- fn debug(lvl: uint , s: &str); // elided
508
- fn debug<'a>(lvl: uint , s: &'a str); // expanded
507
+ fn debug(lvl: u32 , s: &str); // elided
508
+ fn debug<'a>(lvl: u32 , s: &'a str); // expanded
509
509
510
510
// In the preceeding example, `lvl` doesn't need a lifetime because it's not a
511
511
// reference (`&`). Only things relating to references (such as a `struct`
512
512
// which contains a reference) need lifetimes.
513
513
514
- fn substr(s: &str, until: uint ) -> &str; // elided
515
- fn substr<'a>(s: &'a str, until: uint ) -> &'a str; // expanded
514
+ fn substr(s: &str, until: u32 ) -> &str; // elided
515
+ fn substr<'a>(s: &'a str, until: u32 ) -> &'a str; // expanded
516
516
517
517
fn get_str() -> &str; // ILLEGAL, no inputs
518
518
0 commit comments