Skip to content

Commit ade5a9d

Browse files
committed
Fix example in lifetime guide
This rewrites the example to also be more aligned with the same example given in the main tutorial.
1 parent 60b4a97 commit ade5a9d

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/doc/guide-lifetimes.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Now we can call `compute_distance()`:
6767
# let on_the_stack : Point = Point{x: 3.0, y: 4.0};
6868
# let on_the_heap : Box<Point> = box Point{x: 7.0, y: 9.0};
6969
# fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 }
70-
compute_distance(&on_the_stack, &*on_the_heap);
70+
compute_distance(&on_the_stack, on_the_heap);
7171
~~~
7272

7373
Here, the `&` operator takes the address of the variable
@@ -77,9 +77,10 @@ value. We also call this _borrowing_ the local variable
7777
`on_the_stack`, because we have created an alias: that is, another
7878
name for the same data.
7979

80-
For the second argument, we need to extract the contents of `on_the_heap`
81-
by derefercing with the `*` symbol. Now that we have the data, we need
82-
to create a reference with the `&` symbol.
80+
In the case of `on_the_heap`, however, no explicit action is necessary.
81+
The compiler will automatically convert a box box point to a reference like &point.
82+
This is another form of borrowing; in this case, the contents of the owned box
83+
are being lent out.
8384

8485
Whenever a caller lends data to a callee, there are some limitations on what
8586
the caller can do with the original. For example, if the contents of a

0 commit comments

Comments
 (0)