Skip to content

Commit 90943ed

Browse files
committed
---
yaml --- r: 33119 b: refs/heads/dist-snap c: 3077a2b h: refs/heads/master i: 33117: 1d8de58 33115: f1490ed 33111: 0f9ac56 33103: d65c32b 33087: 40c8f47 v: v3
1 parent 1d7091e commit 90943ed

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
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: 38aab8e40034ae21b7c801ae8532ec1f5ae5022d
10+
refs/heads/dist-snap: 3077a2bfaf0bde121072ff71649df6c39f9b66f4
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

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