Skip to content

Commit de4687c

Browse files
committed
Copyedit sections 9-11 of the borrowed pointer tutorial (last edits from me)
1 parent 448c349 commit de4687c

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

doc/tutorial-borrowed-ptr.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,9 @@ points at a static constant).
647647

648648
# Named lifetimes
649649

650-
Let's look at named lifetimes in more detail. In effect, the use of
651-
named lifetimes allows you to group parameters by lifetime. For
652-
example, consider this function:
650+
Let's look at named lifetimes in more detail. Named lifetimes allow
651+
for grouping of parameters by lifetime. For example, consider this
652+
function:
653653

654654
~~~
655655
# struct Point {x: float, y: float}; // as before
@@ -722,10 +722,10 @@ fn select<T>(shape: &tmp/Shape, threshold: float,
722722
}
723723
~~~
724724

725-
Here you can see the lifetime of shape is now being called `tmp`. The
726-
parameters `a`, `b`, and the return value are all given the lifetime
727-
`r`. However, since the lifetime `tmp` is not returned, it would be shorter
728-
to just omit the named lifetime for `shape` altogether:
725+
Here you can see that `shape`'s lifetime is now named `tmp`. The
726+
parameters `a`, `b`, and the return value all have the lifetime `r`.
727+
However, since the lifetime `tmp` is not returned, it would be more
728+
concise to just omit the named lifetime for `shape` altogether:
729729

730730
~~~
731731
# struct Point {x: float, y: float}; // as before
@@ -746,17 +746,22 @@ This is equivalent to the previous definition.
746746
# Purity
747747

748748
As mentioned before, the Rust compiler offers a kind of escape hatch
749-
that permits borrowing of any data, but only if the actions that occur
749+
that permits borrowing of any data, as long as the actions that occur
750750
during the lifetime of the borrow are pure. Pure actions are those
751-
which only modify data owned by the current stack frame. The compiler
751+
that only modify data owned by the current stack frame. The compiler
752752
can therefore permit arbitrary pointers into the heap, secure in the
753753
knowledge that no pure action will ever cause them to become
754754
invalidated (the compiler must still track data on the stack which is
755-
borrowed and enforce those rules normally, of course).
756-
757-
Let’s revisit a previous example and show how purity can affect the
758-
compiler’s result. Here is `example5a()`, which borrows the interior of
759-
a unique box found in an aliasable, mutable location, only now we’ve
755+
borrowed and enforce those rules normally, of course). A pure function
756+
in Rust is referentially transparent: it returns the same results
757+
given the same (observably equivalent) inputs. That is because while
758+
pure functions are allowed to modify data, they may only modify
759+
*stack-local* data, which cannot be observed outside the scope of the
760+
function itself. (Using an `unsafe` block invalidates this guarantee.)
761+
762+
Let’s revisit a previous example and show how purity can affect
763+
typechecking. Here is `example5a()`, which borrows the interior of a
764+
unique box found in an aliasable, mutable location, only now we’ve
760765
replaced the `...` with some specific code:
761766

762767
~~~
@@ -768,8 +773,8 @@ fn example5a(x: @S ...) -> int {
768773
}
769774
~~~
770775

771-
The new code simply returns an incremented version of `y`. This clearly
772-
doesn’t do mutate anything in the heap, so the compiler is satisfied.
776+
The new code simply returns an incremented version of `y`. This code
777+
clearly doesn't mutate the heap, so the compiler is satisfied.
773778

774779
But suppose we wanted to pull the increment code into a helper, like
775780
this:
@@ -791,8 +796,8 @@ fn example5a(x: @S ...) -> int {
791796
~~~
792797

793798
But now the compiler will report an error again. The reason is that it
794-
only considers one function at a time (like most type checkers), and
795-
so it does not know that `add_one()` only takes pure actions. We can
799+
only considers one function at a time (like most typecheckers), and
800+
so it does not know that `add_one()` consists of pure code. We can
796801
help the compiler by labeling `add_one()` as pure:
797802

798803
~~~
@@ -803,7 +808,7 @@ With this change, the modified version of `example5a()` will again compile.
803808

804809
# Conclusion
805810

806-
So there you have it. A (relatively) brief tour of borrowed pointer
807-
system. For more details, I refer to the (yet to be written) reference
811+
So there you have it: a (relatively) brief tour of the borrowed pointer
812+
system. For more details, we refer to the (yet to be written) reference
808813
document on borrowed pointers, which will explain the full notation
809814
and give more examples.

0 commit comments

Comments
 (0)