Skip to content

Commit 484c610

Browse files
authored
Merge pull request #278 from JohnTitor/compile-fail-subtyping
Some improvements on the "subtyping" chapter
2 parents 19f6311 + ae5ad9b commit 484c610

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/subtyping.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ some sense "fundamental". All the others can be understood by analogy to the oth
188188
* `*const T` follows the logic of `&T`
189189
* `*mut T` follows the logic of `&mut T` (or `UnsafeCell<T>`)
190190

191+
For more types, see the ["Variance" section][variance-table] on the reference.
192+
193+
[variance-table]: ../reference/subtyping.html#variance
194+
191195
> NOTE: the *only* source of contravariance in the language is the arguments to
192196
> a function, which is why it really doesn't come up much in practice. Invoking
193197
> contravariance involves higher-order programming with function pointers that
@@ -265,7 +269,7 @@ enough into the place expecting something long-lived.
265269

266270
Here it is:
267271

268-
```rust,ignore
272+
```rust,compile_fail
269273
fn evil_feeder<T>(input: &mut T, val: T) {
270274
*input = val;
271275
}
@@ -285,15 +289,16 @@ And what do we get when we run this?
285289

286290
```text
287291
error[E0597]: `spike` does not live long enough
288-
--> src/main.rs:9:32
292+
--> src/main.rs:9:31
289293
|
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!
293300
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
297302
```
298303

299304
Good, it doesn't compile! Let's break down what's happening here in detail.

0 commit comments

Comments
 (0)