blob: d45663f3a5347732b302d9a637c68dcd9b07f5ed (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
A reference has a longer lifetime than the data it references.
Erroneous code example:
```compile_fail,E0491
struct Foo<'a> {
x: fn(&'a i32),
}
trait Trait<'a, 'b> {
type Out;
}
impl<'a, 'b> Trait<'a, 'b> for usize {
type Out = &'a Foo<'b>; // error!
}
```
Here, the problem is that the compiler cannot be sure that the `'b` lifetime
will live longer than `'a`, which should be mandatory in order to be sure that
`Trait::Out` will always have a reference pointing to an existing type. So in
this case, we just need to tell the compiler than `'b` must outlive `'a`:
```
struct Foo<'a> {
x: fn(&'a i32),
}
trait Trait<'a, 'b> {
type Out;
}
impl<'a, 'b: 'a> Trait<'a, 'b> for usize { // we added the lifetime enforcement
type Out = &'a Foo<'b>; // it now works!
}
```
|