1- % The Rust Borrowed Pointers and Lifetimes Guide
1+ % The Rust References and Lifetimes Guide
22
33# Introduction
44
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
77heap, 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,
99unlike 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
1313management.
1414
15- Despite their complete safety, a borrowed pointer 's representation at runtime
15+ Despite their complete safety, a reference 's representation at runtime
1616is the same as that of an ordinary pointer in a C program. They introduce zero
1717overhead. The compiler does all safety checks at compile time.
1818
19- Although borrowed pointers have rather elaborate theoretical
19+ Although references have rather elaborate theoretical
2020underpinnings (region pointers), the core concepts will be familiar to
2121anyone who has worked with C or C++. Therefore, the best way to explain
2222how they are used—and their limitations—is probably just to work
2323through several examples.
2424
2525# By example
2626
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
2929over the data that they point to: instead, they are used for cases
3030where you would like to use data for a short time.
3131
@@ -55,7 +55,7 @@ define it this way, calling the function will cause the points to be
5555copied. For points, this is probably not so bad, but often copies are
5656expensive. Worse, if the data type contains mutable fields, copying can change
5757the 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
5959this:
6060
6161~~~
@@ -89,7 +89,7 @@ name for the same data.
8989
9090In contrast, we can pass the boxes ` managed_box ` and ` owned_box ` to
9191` 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
9393of borrowing: in this case, the caller lends the contents of the managed or
9494owned box to the callee.
9595
@@ -100,7 +100,7 @@ addition, the compiler will reject any code that might cause the borrowed
100100value to be freed or overwrite its component fields with values of different
101101types (I'll get into what kinds of actions those are shortly). This rule
102102should 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)
104104before you can make full use of it again.
105105
106106# Other uses for the & operator
@@ -114,7 +114,7 @@ let on_the_stack: Point = Point {x: 3.0, y: 4.0};
114114
115115This declaration means that code can only pass ` Point ` by value to other
116116functions. 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
118118convenient to move the & operator into the definition of ` on_the_stack ` :
119119
120120~~~
@@ -180,7 +180,7 @@ as well as from the managed box, and then compute the distance between them.
180180
181181We’ve seen a few examples so far of borrowing heap boxes, both managed
182182and 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
184184is simply a pointer, nothing more. Therefore, avoiding C's problems
185185with dangling pointers requires a compile-time safety check.
186186
@@ -195,7 +195,7 @@ broader scope than the pointer itself), the compiler reports an
195195error. We'll be discussing lifetimes more in the examples to come, and
196196a more thorough introduction is also available.
197197
198- When the ` & ` operator creates a borrowed pointer , the compiler must
198+ When the ` & ` operator creates a reference , the compiler must
199199ensure that the pointer remains valid for its entire
200200lifetime. Sometimes this is relatively easy, such as when taking the
201201address of a local variable or a field that is stored on the stack:
@@ -209,7 +209,7 @@ fn example1() {
209209} // -+
210210~~~
211211
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
213213remainder of the function body. The compiler need not do any other
214214work to prove that code will not free ` x.f ` . This is true even if the
215215code mutates ` x ` .
@@ -384,7 +384,7 @@ enum Shape {
384384~~~
385385
386386Now 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
388388copying.
389389
390390~~~
@@ -466,19 +466,19 @@ same rules as the ones we saw for borrowing the interior of a owned
466466box: it must be able to guarantee that the ` enum ` will not be
467467overwritten for the duration of the borrow. In fact, the compiler
468468would 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
470470immutable memory containing a ` shape ` ". If, however, the type of that
471471pointer were ` &mut Shape ` , then the ref binding would be ill-typed.
472472Just as with owned boxes, the compiler will permit ` ref ` bindings
473473into data owned by the stack frame even if the data are mutable,
474474but otherwise it requires that the data reside in immutable memory.
475475
476- # Returning borrowed pointers
476+ # Returning references
477477
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
479479“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
482482as we'll see, doing so requires some explicit annotation.
483483
484484For example, we could write a subroutine like this:
@@ -496,7 +496,7 @@ explicitly. So in effect, this function declares that it takes a
496496pointer with lifetime ` r ` and returns a pointer with that same
497497lifetime.
498498
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
500500are derived from a parameter to the procedure. In that case, the
501501pointer result will always have the same lifetime as one of the
502502parameters; named lifetimes indicate which parameter that
@@ -532,10 +532,10 @@ fn get_x_sh(p: @Point) -> &f64 {
532532~~~
533533
534534Here, 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
539539subtly different from the first example, which promised to return a
540540pointer that was valid for as long as its pointer argument was valid.
541541
@@ -551,10 +551,10 @@ valid at all once it returns, as the parameter `p` may or may not be
551551live in the caller. Therefore, the compiler will report an error here.
552552
553553In 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
558558points at a static constant).
559559
560560# Named lifetimes
@@ -577,7 +577,7 @@ fn select<'r, T>(shape: &'r Shape, threshold: f64,
577577}
578578~~~
579579
580- This function takes three borrowed pointers and assigns each the same
580+ This function takes three references and assigns each the same
581581lifetime ` r ` . In practice, this means that, in the caller, the
582582lifetime ` r ` will be the * intersection of the lifetime of the three
583583region parameters* . This may be overly conservative, as in this
@@ -657,7 +657,7 @@ This is equivalent to the previous definition.
657657
658658# Conclusion
659659
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
661661system. 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
663663and give more examples.
0 commit comments