Skip to content

Commit 7cfce50

Browse files
huonwbrson
authored andcommitted
Address some minor points in the pointer guide
cc rust-lang#11364.
1 parent 2d8dd6a commit 7cfce50

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

doc/guide-pointers.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Rust's pointers are one of its more unique and compelling features. Pointers
44
are also one of the more confusing topics for newcomers to Rust. They can also
55
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.
77

88
# You don't actually need pointers
99

@@ -13,8 +13,7 @@ that emphasizes safety. Pointers, as the joke goes, are very pointy: it's easy
1313
to accidentally stab yourself. Therefore, Rust is made in a way such that you
1414
don't need them very often.
1515

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:
1817

1918
~~~rust
2019
fn succ(x: &int) -> int { *x + 1 }
@@ -250,6 +249,12 @@ struct.
250249

251250
# Managed Pointers
252251

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+
253258
Managed pointers, notated by an `@`, are used when having a single owner for
254259
some data isn't convenient or possible. This generally happens when your
255260
program is very large and complicated.
@@ -375,12 +380,12 @@ duration a 'lifetime'. Let's try a more complex example:
375380
~~~rust
376381
fn main() {
377382
let mut x = ~5;
378-
if(*x < 10) {
383+
if *x < 10 {
379384
let y = &x;
380385
println!("Oh no: {:?}", y);
381386
return;
382387
}
383-
*x = *x - 1;
388+
*x -= 1;
384389
println!("Oh no: {:?}", x);
385390
}
386391
~~~
@@ -392,14 +397,14 @@ mutated, and therefore, lets us pass. This wouldn't work:
392397
~~~rust {.xfail-test}
393398
fn main() {
394399
let mut x = ~5;
395-
if(*x < 10) {
400+
if *x < 10 {
396401
let y = &x;
397-
*x = *x - 1;
402+
*x -= 1;
398403

399404
println!("Oh no: {:?}", y);
400405
return;
401406
}
402-
*x = *x - 1;
407+
*x -= 1;
403408
println!("Oh no: {:?}", x);
404409
}
405410
~~~
@@ -408,7 +413,7 @@ It gives this error:
408413

409414
~~~ {.notrust}
410415
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;
412417
^~
413418
test.rs:4:16: 4:18 note: borrow of `*x` occurs here
414419
test.rs:4 let y = &x;
@@ -469,8 +474,9 @@ fn main() {
469474
You may think that this gives us terrible performance: return a value and then
470475
immediately box it up?!?! Isn't that the worst of both worlds? Rust is smarter
471476
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.
474480

475481
This is important enough that it bears repeating: pointers are not for optimizing
476482
returning values from your code. Allow the caller to choose how they want to
@@ -479,4 +485,4 @@ use your output.
479485

480486
# Related Resources
481487

482-
* [Lifetimes tutorial](tutorial-lifetimes.html)
488+
* [Lifetimes guide](guide-lifetimes.html)

0 commit comments

Comments
 (0)