Skip to content

Commit 518d3d2

Browse files
huonwbrson
authored andcommitted
---
yaml --- r: 97528 b: refs/heads/snap-stage3 c: 7cfce50 h: refs/heads/master v: v3
1 parent 6f1bd0c commit 518d3d2

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 0da105a8b7b6b1e0568e8ff20f6ff4b13cc7ecc2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 2d8dd6afd4ef36742b4e8a4e848e13ddbe275fe8
4+
refs/heads/snap-stage3: 7cfce50b24cecaa04fae94cbc06ef97a0a9f3958
55
refs/heads/try: c274a6888410ce3e357e014568b43310ed787d36
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/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)