@@ -14,20 +14,19 @@ Despite their complete safety, a reference's representation at runtime
14
14
is the same as that of an ordinary pointer in a C program. They introduce zero
15
15
overhead. The compiler does all safety checks at compile time.
16
16
17
- Although references have rather elaborate theoretical
18
- underpinnings (region pointers), the core concepts will be familiar to
19
- anyone who has worked with C or C++. Therefore, the best way to explain
20
- how they are used—and their limitations—is probably just to work
21
- through several examples.
17
+ Although references have rather elaborate theoretical underpinnings usually
18
+ introduced as (e.g. region pointers), the core concepts will be familiar to
19
+ anyone who has worked with C or C++. The best way to explain how they are
20
+ used—and their limitations—is probably just to work through several examples.
22
21
23
22
# By example
24
23
25
24
References, sometimes known as * borrowed pointers* , are only valid for
26
25
a limited duration. References never claim any kind of ownership
27
- over the data that they point to, instead , they are used for cases
26
+ over the data that they point to. Instead , they are used for cases
28
27
where you would like to use data for a short time.
29
28
30
- As an example, consider a simple struct type ` Point ` :
29
+ Consider a simple struct type ` Point ` :
31
30
32
31
~~~
33
32
struct Point {x: f64, y: f64}
@@ -78,9 +77,9 @@ value. We also call this _borrowing_ the local variable
78
77
` on_the_stack ` , because we have created an alias: that is, another
79
78
name for the same data.
80
79
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 ` &* ` .
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 .
84
83
85
84
Whenever a caller lends data to a callee, there are some limitations on what
86
85
the caller can do with the original. For example, if the contents of a
@@ -194,7 +193,7 @@ fn example3() -> int {
194
193
}
195
194
~~~
196
195
197
- Here, as before, the interior of the variable ` x ` is being borrowed
196
+ Here, the interior of the variable ` x ` is being borrowed
198
197
and ` x ` is declared as mutable. However, the compiler can prove that
199
198
` x ` is not assigned anywhere in the lifetime L of the variable
200
199
` y ` . Therefore, it accepts the function, even though ` x ` is mutable
@@ -281,8 +280,8 @@ prevents pointers from pointing into freed memory. There is one other
281
280
case where the compiler must be very careful to ensure that pointers
282
281
remain valid: pointers into the interior of an ` enum ` .
283
282
284
- As an example, let ’s look at the following ` shape ` type that can
285
- represent both rectangles and circles:
283
+ Let ’s look at the following ` shape ` type that can represent both rectangles
284
+ and circles:
286
285
287
286
~~~
288
287
struct Point {x: f64, y: f64}; // as before
@@ -391,7 +390,7 @@ reference, then uses it within the same scope. It is also
391
390
possible to return references as the result of a function, but
392
391
as we'll see, doing so requires some explicit annotation.
393
392
394
- For example, we could write a subroutine like this:
393
+ We could write a subroutine like this:
395
394
396
395
~~~
397
396
struct Point {x: f64, y: f64}
@@ -412,11 +411,10 @@ pointer result will always have the same lifetime as one of the
412
411
parameters; named lifetimes indicate which parameter that
413
412
is.
414
413
415
- In the previous examples, function parameter types did not include a
416
- lifetime name. In those examples, the compiler simply creates a fresh
417
- name for the lifetime automatically: that is, the lifetime name is
418
- guaranteed to refer to a distinct lifetime from the lifetimes of all
419
- other parameters.
414
+ In the previous code samples, function parameter types did not include a
415
+ lifetime name. The compiler simply creates a fresh name for the lifetime
416
+ automatically: that is, the lifetime name is guaranteed to refer to a distinct
417
+ lifetime from the lifetimes of all other parameters.
420
418
421
419
Named lifetimes that appear in function signatures are conceptually
422
420
the same as the other lifetimes we have seen before, but they are a bit
@@ -461,7 +459,7 @@ guarantees; in fact, it cannot guarantee that the pointer will remain
461
459
valid at all once it returns, as the parameter ` p ` may or may not be
462
460
live in the caller. Therefore, the compiler will report an error here.
463
461
464
- In general, if you borrow a structs or boxes to create a
462
+ In general, if you borrow a struct or box to create a
465
463
reference, it will only be valid within the function
466
464
and cannot be returned. This is why the typical way to return references
467
465
is to take references as input (the only other case in
0 commit comments