Expand E0309 explanation with motivating example

This commit is contained in:
Jon Gjengset 2016-12-11 23:57:15 -05:00
parent 5e2f37fca9
commit f09e2cc7ed
No known key found for this signature in database
GPG key ID: D64AC9D67176DC71

View file

@ -1236,6 +1236,23 @@ struct Foo<'a, T: 'a> {
foo: &'a T
}
```
To see why this is important, consider the case where `T` is itself a reference
(e.g., `T = &str`). If we don't include the restriction that `T: 'a`, the
following code would be perfectly legal:
```compile_fail,E0309
struct Foo<'a, T> {
foo: &'a T
}
fn main() {
let v = "42".to_string();
let f = Foo{foo: &v};
drop(v);
println!("{}", f.foo); // but we've already dropped v!
}
```
"##,
E0310: r##"