Skip to content

Commit 911ed66

Browse files
committed
---
yaml --- r: 22349 b: refs/heads/snap-stage3 c: 3077a2b h: refs/heads/master i: 22347: 759f8c8 v: v3
1 parent be2fa7a commit 911ed66

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
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: e430a699f2c60890d9b86069fd0c68a70ece7120
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 38aab8e40034ae21b7c801ae8532ec1f5ae5022d
4+
refs/heads/snap-stage3: 3077a2bfaf0bde121072ff71649df6c39f9b66f4
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/doc/tutorial.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,73 @@ For a more in-depth explanation of borrowed pointers, read the
12841284
12851285
[borrowtut]: tutorial-borrowed-ptr.html
12861286
1287+
## Dereferencing pointers
1288+
1289+
Rust uses the unary star operator (`*`) to access the contents of a
1290+
box or pointer, similarly to C.
1291+
1292+
~~~
1293+
let managed = @10;
1294+
let owned = ~20;
1295+
let borrowed = &30;
1296+
1297+
let sum = *managed + *owned + *borrowed;
1298+
~~~
1299+
1300+
Dereferenced mutable pointers may appear on the left hand side of
1301+
assignments, in which case the value they point to is modified.
1302+
1303+
~~~
1304+
let managed = @mut 10;
1305+
let owned = ~mut 20;
1306+
1307+
let mut value = 30;
1308+
let borrowed = &mut value;
1309+
1310+
*managed = *owned + 10;
1311+
*owned = *borrowed + 100;
1312+
*borrowed = *managed + 1000;
1313+
~~~
1314+
1315+
Pointers have high operator precedence, but lower precedence than the
1316+
dot operator used for field and method access. This can lead to some
1317+
awkward code filled with parenthesis.
1318+
1319+
~~~
1320+
# struct Point { x: float, y: float }
1321+
# enum Shape { Rectangle(Point, Point) }
1322+
# impl Shape { fn area() -> int { 0 } }
1323+
let start = @Point { x: 10f, y: 20f };
1324+
let end = ~Point { x: (*start).x + 100f, y: (*start).y + 100f };
1325+
let rect = &Rectangle(*start, *end);
1326+
let area = (*rect).area();
1327+
~~~
1328+
1329+
To combat this ugliness the dot operator performs _automatic pointer
1330+
dereferencing_ on the receiver (the value on the left hand side of the
1331+
dot), so in most cases dereferencing the receiver is not necessary.
1332+
1333+
~~~
1334+
# struct Point { x: float, y: float }
1335+
# enum Shape { Rectangle(Point, Point) }
1336+
# impl Shape { fn area() -> int { 0 } }
1337+
let start = @Point { x: 10f, y: 20f };
1338+
let end = ~Point { x: start.x + 100f, y: start.y + 100f };
1339+
let rect = &Rectangle(*start, *end);
1340+
let area = rect.area();
1341+
~~~
1342+
1343+
Auto-dereferencing is performed through any number of pointers. If you
1344+
felt inclined you could write something silly like
1345+
1346+
~~~
1347+
# struct Point { x: float, y: float }
1348+
let point = &@~Point { x: 10f, y: 20f };
1349+
io::println(fmt!("%f", point.x));
1350+
~~~
1351+
1352+
The indexing operator (`[]`) is also auto-dereferencing.
1353+
12871354
# Vectors and strings
12881355
12891356
Vectors are a contiguous section of memory containing zero or more

0 commit comments

Comments
 (0)