@@ -45,12 +45,11 @@ let on_the_heap : Box<Point> = box Point {x: 7.0, y: 9.0};
45
45
46
46
Suppose we wanted to write a procedure that computed the distance between any
47
47
two points, no matter where they were stored. One option is to define a function
48
- that takes two arguments of type ` Point ` —that is, it takes the points __ by value__ .
49
- But if we define it this way, calling the function will cause the points __ to be
50
- copied__ . For points, this is probably not so bad, but often copies are
51
- expensive. Worse, if the data type contains mutable fields, copying can change
52
- the semantics of your program in unexpected ways. So we'd like to define
53
- a function that takes the points just as a __ reference__ /__ borrowed pointer__ .
48
+ that takes two arguments of type ` Point ` —that is, it takes the points by value.
49
+ But if we define it this way, calling the function will cause the points to be
50
+ copied. For points, this is probably not so bad, but often copies are
51
+ expensive. So we'd like to define a function that takes the points just as
52
+ a reference.
54
53
55
54
~~~
56
55
# struct Point {x: f64, y: f64}
@@ -62,27 +61,26 @@ fn compute_distance(p1: &Point, p2: &Point) -> f64 {
62
61
}
63
62
~~~
64
63
65
- Now we can call ` compute_distance() `
64
+ Now we can call ` compute_distance() ` :
66
65
67
66
~~~
68
67
# struct Point {x: f64, y: f64}
69
68
# let on_the_stack : Point = Point{x: 3.0, y: 4.0};
70
69
# let on_the_heap : Box<Point> = box Point{x: 7.0, y: 9.0};
71
70
# fn compute_distance(p1: &Point, p2: &Point) -> f64 { 0.0 }
72
- compute_distance(&on_the_stack, on_the_heap);
71
+ compute_distance(&on_the_stack, &* on_the_heap);
73
72
~~~
74
73
75
74
Here, the ` & ` operator takes the address of the variable
76
75
` on_the_stack ` ; this is because ` on_the_stack ` has the type ` Point `
77
76
(that is, a struct value) and we have to take its address to get a
78
77
value. We also call this _ borrowing_ the local variable
79
- ` on_the_stack ` , because we have created __ an alias __ : that is, another
78
+ ` on_the_stack ` , because we have created an alias : that is, another
80
79
name for the same data.
81
80
82
- In contrast, we can pass ` on_the_heap ` to ` compute_distance ` directly.
83
- The compiler automatically converts a box like ` Box<Point> ` to a reference like
84
- ` &Point ` . This is another form of borrowing: in this case, the caller lends
85
- the contents of the box to the callee.
81
+ For the second argument, we need to grab the contents of ` on_the_heap `
82
+ by using the ` * ` operator, and then get a reference to that data. In
83
+ order to convert ` Box<T> ` into a ` &T ` , we need to use ` &* ` .
86
84
87
85
Whenever a caller lends data to a callee, there are some limitations on what
88
86
the caller can do with the original. For example, if the contents of a
@@ -166,12 +164,12 @@ as well as from the owned box, and then compute the distance between them.
166
164
167
165
# Lifetimes
168
166
169
- We’ve seen a few examples of borrowing data. Up till this point, we’ve glossed
167
+ We’ve seen a few examples of borrowing data. To this point, we’ve glossed
170
168
over issues of safety. As stated in the introduction, at runtime a reference
171
169
is simply a pointer, nothing more. Therefore, avoiding C's problems with
172
170
dangling pointers requires a compile-time safety check.
173
171
174
- The basis for the check is the notion of __ lifetimes __ . A lifetime is a
172
+ The basis for the check is the notion of _ lifetimes _ . A lifetime is a
175
173
static approximation of the span of execution during which the pointer
176
174
is valid: it always corresponds to some expression or block within the
177
175
program.
@@ -324,7 +322,7 @@ circle constant][tau] and not that dreadfully outdated notion of pi).
324
322
325
323
The second match is more interesting. Here we match against a
326
324
rectangle and extract its size: but rather than copy the ` size `
327
- struct, we use a __ by -reference binding __ to create a pointer to it. In
325
+ struct, we use a by -reference binding to create a pointer to it. In
328
326
other words, a pattern binding like ` ref size ` binds the name ` size `
329
327
to a pointer of type ` &size ` into the _ interior of the enum_ .
330
328
0 commit comments