1
- % The Rust Borrowed Pointers and Lifetimes Guide
1
+ % The Rust References and Lifetimes Guide
2
2
3
3
# Introduction
4
4
5
- Borrowed pointers are one of the more flexible and powerful tools available in
6
- Rust. A borrowed pointer can point anywhere: into the managed or exchange
5
+ References are one of the more flexible and powerful tools available in
6
+ Rust. A reference can point anywhere: into the managed or exchange
7
7
heap, into the stack, and even into the interior of another data structure. A
8
- borrowed pointer is as flexible as a C pointer or C++ reference. However,
8
+ reference is as flexible as a C pointer or C++ reference. However,
9
9
unlike C and C++ compilers, the Rust compiler includes special static checks
10
- that ensure that programs use borrowed pointers safely. Another advantage of
11
- borrowed pointers is that they are invisible to the garbage collector, so
12
- working with borrowed pointers helps reduce the overhead of automatic memory
10
+ that ensure that programs use references safely. Another advantage of
11
+ references is that they are invisible to the garbage collector, so
12
+ working with references helps reduce the overhead of automatic memory
13
13
management.
14
14
15
- Despite their complete safety, a borrowed pointer 's representation at runtime
15
+ Despite their complete safety, a reference 's representation at runtime
16
16
is the same as that of an ordinary pointer in a C program. They introduce zero
17
17
overhead. The compiler does all safety checks at compile time.
18
18
19
- Although borrowed pointers have rather elaborate theoretical
19
+ Although references have rather elaborate theoretical
20
20
underpinnings (region pointers), the core concepts will be familiar to
21
21
anyone who has worked with C or C++. Therefore, the best way to explain
22
22
how they are used—and their limitations—is probably just to work
23
23
through several examples.
24
24
25
25
# By example
26
26
27
- Borrowed pointers are called * borrowed* because they are only valid for
28
- a limited duration. Borrowed pointers never claim any kind of ownership
27
+ References, sometimes known as * borrowed pointers * , are only valid for
28
+ a limited duration. References never claim any kind of ownership
29
29
over the data that they point to: instead, they are used for cases
30
30
where you would like to use data for a short time.
31
31
@@ -55,7 +55,7 @@ define it this way, calling the function will cause the points to be
55
55
copied. For points, this is probably not so bad, but often copies are
56
56
expensive. Worse, if the data type contains mutable fields, copying can change
57
57
the semantics of your program in unexpected ways. So we'd like to define a
58
- function that takes the points by pointer. We can use borrowed pointers to do
58
+ function that takes the points by pointer. We can use references to do
59
59
this:
60
60
61
61
~~~
@@ -89,7 +89,7 @@ name for the same data.
89
89
90
90
In contrast, we can pass the boxes ` managed_box ` and ` owned_box ` to
91
91
` compute_distance ` directly. The compiler automatically converts a box like
92
- ` @Point ` or ` ~Point ` to a borrowed pointer like ` &Point ` . This is another form
92
+ ` @Point ` or ` ~Point ` to a reference like ` &Point ` . This is another form
93
93
of borrowing: in this case, the caller lends the contents of the managed or
94
94
owned box to the callee.
95
95
@@ -100,7 +100,7 @@ addition, the compiler will reject any code that might cause the borrowed
100
100
value to be freed or overwrite its component fields with values of different
101
101
types (I'll get into what kinds of actions those are shortly). This rule
102
102
should make intuitive sense: you must wait for a borrower to return the value
103
- that you lent it (that is, wait for the borrowed pointer to go out of scope)
103
+ that you lent it (that is, wait for the reference to go out of scope)
104
104
before you can make full use of it again.
105
105
106
106
# Other uses for the & operator
@@ -114,7 +114,7 @@ let on_the_stack: Point = Point {x: 3.0, y: 4.0};
114
114
115
115
This declaration means that code can only pass ` Point ` by value to other
116
116
functions. As a consequence, we had to explicitly take the address of
117
- ` on_the_stack ` to get a borrowed pointer . Sometimes however it is more
117
+ ` on_the_stack ` to get a reference . Sometimes however it is more
118
118
convenient to move the & operator into the definition of ` on_the_stack ` :
119
119
120
120
~~~
@@ -180,7 +180,7 @@ as well as from the managed box, and then compute the distance between them.
180
180
181
181
We’ve seen a few examples so far of borrowing heap boxes, both managed
182
182
and owned. Up till this point, we’ve glossed over issues of
183
- safety. As stated in the introduction, at runtime a borrowed pointer
183
+ safety. As stated in the introduction, at runtime a reference
184
184
is simply a pointer, nothing more. Therefore, avoiding C's problems
185
185
with dangling pointers requires a compile-time safety check.
186
186
@@ -195,7 +195,7 @@ broader scope than the pointer itself), the compiler reports an
195
195
error. We'll be discussing lifetimes more in the examples to come, and
196
196
a more thorough introduction is also available.
197
197
198
- When the ` & ` operator creates a borrowed pointer , the compiler must
198
+ When the ` & ` operator creates a reference , the compiler must
199
199
ensure that the pointer remains valid for its entire
200
200
lifetime. Sometimes this is relatively easy, such as when taking the
201
201
address of a local variable or a field that is stored on the stack:
@@ -209,7 +209,7 @@ fn example1() {
209
209
} // -+
210
210
~~~
211
211
212
- Here, the lifetime of the borrowed pointer ` y ` is simply L, the
212
+ Here, the lifetime of the reference ` y ` is simply L, the
213
213
remainder of the function body. The compiler need not do any other
214
214
work to prove that code will not free ` x.f ` . This is true even if the
215
215
code mutates ` x ` .
@@ -384,7 +384,7 @@ enum Shape {
384
384
~~~
385
385
386
386
Now we might write a function to compute the area of a shape. This
387
- function takes a borrowed pointer to a shape, to avoid the need for
387
+ function takes a reference to a shape, to avoid the need for
388
388
copying.
389
389
390
390
~~~
@@ -466,19 +466,19 @@ same rules as the ones we saw for borrowing the interior of a owned
466
466
box: it must be able to guarantee that the ` enum ` will not be
467
467
overwritten for the duration of the borrow. In fact, the compiler
468
468
would accept the example we gave earlier. The example is safe because
469
- the shape pointer has type ` &Shape ` , which means "borrowed pointer to
469
+ the shape pointer has type ` &Shape ` , which means "reference to
470
470
immutable memory containing a ` shape ` ". If, however, the type of that
471
471
pointer were ` &mut Shape ` , then the ref binding would be ill-typed.
472
472
Just as with owned boxes, the compiler will permit ` ref ` bindings
473
473
into data owned by the stack frame even if the data are mutable,
474
474
but otherwise it requires that the data reside in immutable memory.
475
475
476
- # Returning borrowed pointers
476
+ # Returning references
477
477
478
- So far, all of the examples we have looked at, use borrowed pointers in a
478
+ So far, all of the examples we have looked at, use references in a
479
479
“downward” direction. That is, a method or code block creates a
480
- borrowed pointer , then uses it within the same scope. It is also
481
- possible to return borrowed pointers as the result of a function, but
480
+ reference , then uses it within the same scope. It is also
481
+ possible to return references as the result of a function, but
482
482
as we'll see, doing so requires some explicit annotation.
483
483
484
484
For example, we could write a subroutine like this:
@@ -496,7 +496,7 @@ explicitly. So in effect, this function declares that it takes a
496
496
pointer with lifetime ` r ` and returns a pointer with that same
497
497
lifetime.
498
498
499
- In general, it is only possible to return borrowed pointers if they
499
+ In general, it is only possible to return references if they
500
500
are derived from a parameter to the procedure. In that case, the
501
501
pointer result will always have the same lifetime as one of the
502
502
parameters; named lifetimes indicate which parameter that
@@ -532,10 +532,10 @@ fn get_x_sh(p: @Point) -> &f64 {
532
532
~~~
533
533
534
534
Here, the function ` get_x_sh() ` takes a managed box as input and
535
- returns a borrowed pointer . As before, the lifetime of the borrowed
536
- pointer that will be returned is a parameter (specified by the
537
- caller). That means that ` get_x_sh() ` promises to return a borrowed
538
- pointer that is valid for as long as the caller would like: this is
535
+ returns a reference . As before, the lifetime of the reference
536
+ that will be returned is a parameter (specified by the
537
+ caller). That means that ` get_x_sh() ` promises to return a reference
538
+ that is valid for as long as the caller would like: this is
539
539
subtly different from the first example, which promised to return a
540
540
pointer that was valid for as long as its pointer argument was valid.
541
541
@@ -551,10 +551,10 @@ valid at all once it returns, as the parameter `p` may or may not be
551
551
live in the caller. Therefore, the compiler will report an error here.
552
552
553
553
In general, if you borrow a managed (or owned) box to create a
554
- borrowed pointer, the pointer will only be valid within the function
555
- and cannot be returned. This is why the typical way to return borrowed
556
- pointers is to take borrowed pointers as input (the only other case in
557
- which it can be legal to return a borrowed pointer is if the pointer
554
+ reference, it will only be valid within the function
555
+ and cannot be returned. This is why the typical way to return references
556
+ is to take references as input (the only other case in
557
+ which it can be legal to return a reference is if it
558
558
points at a static constant).
559
559
560
560
# Named lifetimes
@@ -577,7 +577,7 @@ fn select<'r, T>(shape: &'r Shape, threshold: f64,
577
577
}
578
578
~~~
579
579
580
- This function takes three borrowed pointers and assigns each the same
580
+ This function takes three references and assigns each the same
581
581
lifetime ` r ` . In practice, this means that, in the caller, the
582
582
lifetime ` r ` will be the * intersection of the lifetime of the three
583
583
region parameters* . This may be overly conservative, as in this
@@ -657,7 +657,7 @@ This is equivalent to the previous definition.
657
657
658
658
# Conclusion
659
659
660
- So there you have it: a (relatively) brief tour of the borrowed pointer
660
+ So there you have it: a (relatively) brief tour of the lifetime
661
661
system. For more details, we refer to the (yet to be written) reference
662
- document on borrowed pointers , which will explain the full notation
662
+ document on references , which will explain the full notation
663
663
and give more examples.
0 commit comments