3
3
Rust's pointers are one of its more unique and compelling features. Pointers
4
4
are also one of the more confusing topics for newcomers to Rust. They can also
5
5
be confusing for people coming from other languages that support pointers, such
6
- as C++. This tutorial will help you understand this important topic.
6
+ as C++. This guide will help you understand this important topic.
7
7
8
8
# You don't actually need pointers
9
9
@@ -13,8 +13,7 @@ that emphasizes safety. Pointers, as the joke goes, are very pointy: it's easy
13
13
to accidentally stab yourself. Therefore, Rust is made in a way such that you
14
14
don't need them very often.
15
15
16
- "But tutorial!" you may cry. "My co-worker wrote a function that looks like
17
- this:
16
+ "But guide!" you may cry. "My co-worker wrote a function that looks like this:
18
17
19
18
~~~ rust
20
19
fn succ (x : & int ) -> int { * x + 1 }
@@ -250,6 +249,12 @@ struct.
250
249
251
250
# Managed Pointers
252
251
252
+ > ** Note** : the ` @ ` form of managed pointers is deprecated and behind a
253
+ > feature gate (it requires a ` #[feature(managed_pointers)]; ` attribute on
254
+ > the crate root; remember the semicolon!). There are replacements, currently
255
+ > there is ` std::rc::Rc ` and ` std::gc::Gc ` for shared ownership via reference
256
+ > counting and garbage collection respectively.
257
+
253
258
Managed pointers, notated by an ` @ ` , are used when having a single owner for
254
259
some data isn't convenient or possible. This generally happens when your
255
260
program is very large and complicated.
@@ -375,12 +380,12 @@ duration a 'lifetime'. Let's try a more complex example:
375
380
~~~ rust
376
381
fn main () {
377
382
let mut x = ~5 ;
378
- if ( * x < 10 ) {
383
+ if * x < 10 {
379
384
let y = & x ;
380
385
println! (" Oh no: {:?}" , y );
381
386
return ;
382
387
}
383
- * x = * x - 1 ;
388
+ * x -= 1 ;
384
389
println! (" Oh no: {:?}" , x );
385
390
}
386
391
~~~
@@ -392,14 +397,14 @@ mutated, and therefore, lets us pass. This wouldn't work:
392
397
~~~ rust {.xfail-test}
393
398
fn main () {
394
399
let mut x = ~5 ;
395
- if ( * x < 10 ) {
400
+ if * x < 10 {
396
401
let y = & x ;
397
- * x = * x - 1 ;
402
+ * x -= 1 ;
398
403
399
404
println! (" Oh no: {:?}" , y );
400
405
return ;
401
406
}
402
- * x = * x - 1 ;
407
+ * x -= 1 ;
403
408
println! (" Oh no: {:?}" , x );
404
409
}
405
410
~~~
@@ -408,7 +413,7 @@ It gives this error:
408
413
409
414
~~~ {.notrust}
410
415
test.rs:5:8: 5:10 error: cannot assign to `*x` because it is borrowed
411
- test.rs:5 *x = *x - 1;
416
+ test.rs:5 *x -= 1;
412
417
^~
413
418
test.rs:4:16: 4:18 note: borrow of `*x` occurs here
414
419
test.rs:4 let y = &x;
@@ -469,8 +474,9 @@ fn main() {
469
474
You may think that this gives us terrible performance: return a value and then
470
475
immediately box it up?!?! Isn't that the worst of both worlds? Rust is smarter
471
476
than that. There is no copy in this code. ` main ` allocates enough room for the
472
- ` @int ` , passes it into ` foo ` as ` x ` , and then ` foo ` writes the value into the
473
- new box. This writes the return value directly into the allocated box.
477
+ ` @int ` , passes a pointer to that memory into ` foo ` as ` x ` , and then ` foo ` writes
478
+ the value straight into that pointer. This writes the return value directly into
479
+ the allocated box.
474
480
475
481
This is important enough that it bears repeating: pointers are not for optimizing
476
482
returning values from your code. Allow the caller to choose how they want to
@@ -479,4 +485,4 @@ use your output.
479
485
480
486
# Related Resources
481
487
482
- * [ Lifetimes tutorial ] ( tutorial -lifetimes.html)
488
+ * [ Lifetimes guide ] ( guide -lifetimes.html)
0 commit comments