2
2
3
3
We have seen how lifetimes provide us some fairly simple rules for ensuring
4
4
that we never read dangling references. However up to this point we have only ever
5
- interacted with the * outlives * relationship in an inclusive manner. That is,
6
- when we talked about ` 'a: 'b ` , it was ok for ` 'a ` to live * exactly * as long as
5
+ interacted with the _ outlives _ relationship in an inclusive manner. That is,
6
+ when we talked about ` 'a: 'b ` , it was ok for ` 'a ` to live _ exactly _ as long as
7
7
` 'b ` . At first glance, this seems to be a meaningless distinction. Nothing ever
8
8
gets dropped at the same time as another, right? This is why we used the
9
9
following desugaring of ` let ` statements:
@@ -35,7 +35,7 @@ let tuple = (vec![], vec![]);
35
35
36
36
The left vector is dropped first. But does it mean the right one strictly
37
37
outlives it in the eyes of the borrow checker? The answer to this question is
38
- * no * . The borrow checker could track fields of tuples separately, but it would
38
+ _ no _ . The borrow checker could track fields of tuples separately, but it would
39
39
still be unable to decide what outlives what in case of vector elements, which
40
40
are dropped manually via pure-library code the borrow checker doesn't
41
41
understand.
@@ -93,15 +93,16 @@ fn main() {
93
93
94
94
``` text
95
95
error[E0597]: `world.days` does not live long enough
96
- --> src/main.rs:20:39
96
+ --> src/main.rs:19:38
97
97
|
98
- 20 | world.inspector = Some(Inspector(&world.days));
99
- | ^^^^^^^^^^ borrowed value does not live long enough
98
+ 19 | world.inspector = Some(Inspector(&world.days));
99
+ | ^ ^^^^^^^^^^ borrowed value does not live long enough
100
100
...
101
- 23 | }
102
- | - `world.days` dropped here while still borrowed
103
- |
104
- = note: values in a scope are dropped in the opposite order they are created
101
+ 22 | }
102
+ | -
103
+ | |
104
+ | `world.days` dropped here while still borrowed
105
+ | borrow might be used here, when `world` is dropped and runs the destructor for type `World<'_>`
105
106
```
106
107
107
108
You can try changing the order of fields or use a tuple instead of the struct,
@@ -113,8 +114,8 @@ live as long as it does actually were destroyed first.
113
114
114
115
Interestingly, only generic types need to worry about this. If they aren't
115
116
generic, then the only lifetimes they can harbor are ` 'static ` , which will truly
116
- live * forever * . This is why this problem is referred to as * sound generic drop * .
117
- Sound generic drop is enforced by the * drop checker * . As of this writing, some
117
+ live _ forever _ . This is why this problem is referred to as _ sound generic drop _ .
118
+ Sound generic drop is enforced by the _ drop checker _ . As of this writing, some
118
119
of the finer details of how the drop checker validates types is totally up in
119
120
the air. However The Big Rule is the subtlety that we have focused on this whole
120
121
section:
@@ -190,12 +191,12 @@ fn main() {
190
191
}
191
192
```
192
193
193
- However, * both * of the above variants are rejected by the borrow
194
+ However, _ both _ of the above variants are rejected by the borrow
194
195
checker during the analysis of ` fn main ` , saying that ` days ` does not
195
196
live long enough.
196
197
197
198
The reason is that the borrow checking analysis of ` main ` does not
198
- know about the internals of each ` Inspector ` 's ` Drop ` implementation. As
199
+ know about the internals of each ` Inspector ` 's ` Drop ` implementation. As
199
200
far as the borrow checker knows while it is analyzing ` main ` , the body
200
201
of an inspector's destructor might access that borrowed data.
201
202
@@ -216,7 +217,7 @@ This would help address cases such as the two `Inspector`s above that
216
217
know not to inspect during destruction.
217
218
218
219
In the meantime, there is an unstable attribute that one can use to
219
- assert (unsafely) that a generic type's destructor is * guaranteed * to
220
+ assert (unsafely) that a generic type's destructor is _ guaranteed _ to
220
221
not access any expired data, even if its type gives it the capability
221
222
to do so.
222
223
@@ -274,8 +275,8 @@ It is sometimes obvious that no such access can occur, like the case above.
274
275
However, when dealing with a generic type parameter, such access can
275
276
occur indirectly. Examples of such indirect access are:
276
277
277
- * invoking a callback,
278
- * via a trait method call.
278
+ - invoking a callback,
279
+ - via a trait method call.
279
280
280
281
(Future changes to the language, such as impl specialization, may add
281
282
other avenues for such indirect access.)
@@ -334,7 +335,6 @@ worry at all about doing the right thing for the drop checker. However there
334
335
is one special case that you need to worry about, which we will look at in
335
336
the next section.
336
337
337
-
338
338
[ rfc1327 ] : https://github.com/rust-lang/rfcs/blob/master/text/1327-dropck-param-eyepatch.md
339
339
[ rfc1857 ] : https://github.com/rust-lang/rfcs/blob/master/text/1857-stabilize-drop-order.md
340
- [ `ManuallyDrop ` ] : ../std/mem/struct.ManuallyDrop.html
340
+ [ `manuallydrop ` ] : ../std/mem/struct.ManuallyDrop.html
0 commit comments