@@ -188,6 +188,10 @@ some sense "fundamental". All the others can be understood by analogy to the oth
188
188
* ` *const T ` follows the logic of ` &T `
189
189
* ` *mut T ` follows the logic of ` &mut T ` (or ` UnsafeCell<T> ` )
190
190
191
+ For more types, see the [ "Variance" section] [ variance-table ] on the reference.
192
+
193
+ [ variance-table ] : ../reference/subtyping.html#variance
194
+
191
195
> NOTE: the * only* source of contravariance in the language is the arguments to
192
196
> a function, which is why it really doesn't come up much in practice. Invoking
193
197
> contravariance involves higher-order programming with function pointers that
@@ -265,7 +269,7 @@ enough into the place expecting something long-lived.
265
269
266
270
Here it is:
267
271
268
- ``` rust,ignore
272
+ ``` rust,compile_fail
269
273
fn evil_feeder<T>(input: &mut T, val: T) {
270
274
*input = val;
271
275
}
@@ -285,15 +289,16 @@ And what do we get when we run this?
285
289
286
290
``` text
287
291
error[E0597]: `spike` does not live long enough
288
- --> src/main.rs:9:32
292
+ --> src/main.rs:9:31
289
293
|
290
- 9 | let spike_str: &str = &spike;
291
- | ^^^^^ borrowed value does not live long enough
292
- 10 | evil_feeder(&mut mr_snuggles, spike_str);
294
+ 6 | let mut mr_snuggles: &'static str = "meow! :3"; // mr. snuggles forever!!
295
+ | ------------ type annotation requires that `spike` is borrowed for `'static`
296
+ ...
297
+ 9 | let spike_str: &str = &spike; // Only lives for the block
298
+ | ^^^^^^ borrowed value does not live long enough
299
+ 10 | evil_feeder(&mut mr_snuggles, spike_str); // EVIL!
293
300
11 | }
294
- | - borrowed value only lives until here
295
- |
296
- = note: borrowed value must be valid for the static lifetime...
301
+ | - `spike` dropped here while still borrowed
297
302
```
298
303
299
304
Good, it doesn't compile! Let's break down what's happening here in detail.
0 commit comments