Skip to content

Commit d323632

Browse files
committed
'borrowed pointer' -> 'reference'
1 parent b65b4d6 commit d323632

39 files changed

+185
-186
lines changed

doc/complement-lang-faq.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ They start small (ideally in the hundreds of bytes) and expand dynamically by ca
218218
* Managed boxes may not be shared between tasks
219219
* Owned boxes may be transferred (moved) between tasks
220220

221-
## What is the difference between a borrowed pointer (`&`) and managed and owned boxes?
221+
## What is the difference between a reference (`&`) and managed and owned boxes?
222222

223-
* Borrowed pointers point to the interior of a stack _or_ heap allocation
224-
* Borrowed pointers can only be formed when it will provably be outlived by the referent
225-
* Borrowed pointers to managed box pointers keep the managed boxes alive
226-
* Borrowed pointers to owned boxes prevent their ownership from being transferred
227-
* Borrowed pointers employ region-based alias analysis to ensure correctness
223+
* References point to the interior of a stack _or_ heap allocation
224+
* References can only be formed when it will provably be outlived by the referent
225+
* References to managed box pointers keep the managed boxes alive
226+
* References to owned boxes prevent their ownership from being transferred
227+
* References employ region-based alias analysis to ensure correctness
228228

229229
## Why aren't function signatures inferred? Why only local slots?
230230

doc/guide-ffi.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ However, there are currently no guarantees about the layout of an `enum`.
417417

418418
Rust's owned and managed boxes use non-nullable pointers as handles which point to the contained
419419
object. However, they should not be manually created because they are managed by internal
420-
allocators. Borrowed pointers can safely be assumed to be non-nullable pointers directly to the
420+
allocators. References can safely be assumed to be non-nullable pointers directly to the
421421
type. However, breaking the borrow checking or mutability rules is not guaranteed to be safe, so
422422
prefer using raw pointers (`*`) if that's needed because the compiler can't make as many assumptions
423423
about them.

doc/guide-lifetimes.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
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
77
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,
99
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
1313
management.
1414

15-
Despite their complete safety, a borrowed pointer's representation at runtime
15+
Despite their complete safety, a reference's representation at runtime
1616
is the same as that of an ordinary pointer in a C program. They introduce zero
1717
overhead. 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
2020
underpinnings (region pointers), the core concepts will be familiar to
2121
anyone who has worked with C or C++. Therefore, the best way to explain
2222
how they are used—and their limitations—is probably just to work
2323
through 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
2929
over the data that they point to: instead, they are used for cases
3030
where 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
5555
copied. For points, this is probably not so bad, but often copies are
5656
expensive. Worse, if the data type contains mutable fields, copying can change
5757
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
5959
this:
6060

6161
~~~
@@ -89,7 +89,7 @@ name for the same data.
8989

9090
In 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
9393
of borrowing: in this case, the caller lends the contents of the managed or
9494
owned box to the callee.
9595

@@ -100,7 +100,7 @@ addition, the compiler will reject any code that might cause the borrowed
100100
value to be freed or overwrite its component fields with values of different
101101
types (I'll get into what kinds of actions those are shortly). This rule
102102
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)
104104
before 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

115115
This declaration means that code can only pass `Point` by value to other
116116
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
118118
convenient 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

181181
We’ve seen a few examples so far of borrowing heap boxes, both managed
182182
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
184184
is simply a pointer, nothing more. Therefore, avoiding C's problems
185185
with dangling pointers requires a compile-time safety check.
186186

@@ -195,7 +195,7 @@ broader scope than the pointer itself), the compiler reports an
195195
error. We'll be discussing lifetimes more in the examples to come, and
196196
a 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
199199
ensure that the pointer remains valid for its entire
200200
lifetime. Sometimes this is relatively easy, such as when taking the
201201
address 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
213213
remainder of the function body. The compiler need not do any other
214214
work to prove that code will not free `x.f`. This is true even if the
215215
code mutates `x`.
@@ -384,7 +384,7 @@ enum Shape {
384384
~~~
385385

386386
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
388388
copying.
389389

390390
~~~
@@ -466,19 +466,19 @@ same rules as the ones we saw for borrowing the interior of a owned
466466
box: it must be able to guarantee that the `enum` will not be
467467
overwritten for the duration of the borrow. In fact, the compiler
468468
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
470470
immutable memory containing a `shape`". If, however, the type of that
471471
pointer were `&mut Shape`, then the ref binding would be ill-typed.
472472
Just as with owned boxes, the compiler will permit `ref` bindings
473473
into data owned by the stack frame even if the data are mutable,
474474
but 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
482482
as we'll see, doing so requires some explicit annotation.
483483

484484
For example, we could write a subroutine like this:
@@ -496,7 +496,7 @@ explicitly. So in effect, this function declares that it takes a
496496
pointer with lifetime `r` and returns a pointer with that same
497497
lifetime.
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
500500
are derived from a parameter to the procedure. In that case, the
501501
pointer result will always have the same lifetime as one of the
502502
parameters; named lifetimes indicate which parameter that
@@ -532,10 +532,10 @@ fn get_x_sh(p: @Point) -> &f64 {
532532
~~~
533533

534534
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
539539
subtly different from the first example, which promised to return a
540540
pointer 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
551551
live in the caller. Therefore, the compiler will report an error here.
552552

553553
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
558558
points 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
581581
lifetime `r`. In practice, this means that, in the caller, the
582582
lifetime `r` will be the *intersection of the lifetime of the three
583583
region 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
661661
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
663663
and give more examples.

doc/guide-pointers.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ common, such as C++, please read "A note..." below.
8686
or impossible. This is only often useful when a program is very large or very
8787
complicated. Using a managed pointer will activate Rust's garbage collection
8888
mechanism.
89-
5: Borrowed: You're writing a function, and you need a pointer, but you don't
90-
care about its ownership. If you make the argument a borrowed pointer, callers
89+
5: Reference: You're writing a function, and you need a pointer, but you don't
90+
care about its ownership. If you make the argument a reference, callers
9191
can send in whatever kind they want.
9292

9393
Five exceptions. That's it. Otherwise, you shouldn't need them. Be skeptical
@@ -313,11 +313,11 @@ drawback.
313313
concurrency boundaries is the source of endless pain in other langauges, so
314314
Rust does not let you do this.
315315

316-
# Borrowed Pointers
316+
# References
317317

318-
Borrowed pointers are the third major kind of pointer Rust supports. They are
318+
References are the third major kind of pointer Rust supports. They are
319319
simultaneously the simplest and the most complicated kind. Let me explain:
320-
they're called 'borrowed' pointers because they claim no ownership over the
320+
references are considered 'borrowed' because they claim no ownership over the
321321
data they're pointing to. They're just borrowing it for a while. So in that
322322
sense, they're simple: just keep whatever ownership the data already has. For
323323
example:
@@ -346,13 +346,13 @@ fn main() {
346346
~~~
347347

348348
This prints `5.83095189`. You can see that the `compute_distance` function
349-
takes in two borrowed pointers, but we give it a managed and unique pointer. Of
349+
takes in two references, but we give it a managed and unique pointer. Of
350350
course, if this were a real program, we wouldn't have any of these pointers,
351351
they're just there to demonstrate the concepts.
352352

353353
So how is this hard? Well, because we're igorning ownership, the compiler needs
354354
to take great care to make sure that everything is safe. Despite their complete
355-
safety, a borrowed pointer's representation at runtime is the same as that of
355+
safety, a reference's representation at runtime is the same as that of
356356
an ordinary pointer in a C program. They introduce zero overhead. The compiler
357357
does all safety checks at compile time.
358358

@@ -416,8 +416,8 @@ test.rs:4 let y = &x;
416416
~~~
417417

418418
As you might guess, this kind of analysis is complex for a human, and therefore
419-
hard for a computer, too! There is an entire [tutorial devoted to borrowed
420-
pointers and lifetimes](tutorial-lifetimes.html) that goes into lifetimes in
419+
hard for a computer, too! There is an entire [guide devoted to references
420+
and lifetimes](guide-lifetimes.html) that goes into lifetimes in
421421
great detail, so if you want the full details, check that out.
422422

423423
# Returning Pointers

doc/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Guides
1313

1414
[Pointers](guide-pointers.html)
15-
[Lifetimes](guide-lifetimes.html)
15+
[References and Lifetimes](guide-lifetimes.html)
1616
[Containers and Iterators](guide-container.html)
1717
[Tasks and Communication](guide-tasks.html)
1818
[Foreign Function Interface](guide-ffi.html)

0 commit comments

Comments
 (0)