@@ -647,9 +647,9 @@ points at a static constant).
647
647
648
648
# Named lifetimes
649
649
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:
653
653
654
654
~~~
655
655
# struct Point {x: float, y: float}; // as before
@@ -722,10 +722,10 @@ fn select<T>(shape: &tmp/Shape, threshold: float,
722
722
}
723
723
~~~
724
724
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:
729
729
730
730
~~~
731
731
# struct Point {x: float, y: float}; // as before
@@ -746,17 +746,22 @@ This is equivalent to the previous definition.
746
746
# Purity
747
747
748
748
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
750
750
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
752
752
can therefore permit arbitrary pointers into the heap, secure in the
753
753
knowledge that no pure action will ever cause them to become
754
754
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
760
765
replaced the ` ... ` with some specific code:
761
766
762
767
~~~
@@ -768,8 +773,8 @@ fn example5a(x: @S ...) -> int {
768
773
}
769
774
~~~
770
775
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.
773
778
774
779
But suppose we wanted to pull the increment code into a helper, like
775
780
this:
@@ -791,8 +796,8 @@ fn example5a(x: @S ...) -> int {
791
796
~~~
792
797
793
798
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
796
801
help the compiler by labeling ` add_one() ` as pure:
797
802
798
803
~~~
@@ -803,7 +808,7 @@ With this change, the modified version of `example5a()` will again compile.
803
808
804
809
# Conclusion
805
810
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
808
813
document on borrowed pointers, which will explain the full notation
809
814
and give more examples.
0 commit comments