]>
Commit | Line | Data |
---|---|---|
74b04a01 XL |
1 | A parameter type is missing a lifetime constraint or has a lifetime that |
2 | does not live long enough. | |
3 | ||
4 | Erroneous code example: | |
60c5eb7d XL |
5 | |
6 | ```compile_fail,E0310 | |
7 | // This won't compile because T is not constrained to the static lifetime | |
8 | // the reference needs | |
9 | struct Foo<T> { | |
10 | foo: &'static T | |
11 | } | |
12 | ``` | |
13 | ||
74b04a01 XL |
14 | Type parameters in type definitions have lifetimes associated with them that |
15 | represent how long the data stored within them is guaranteed to live. This | |
16 | lifetime must be as long as the data needs to be alive, and missing the | |
17 | constraint that denotes this will cause this error. | |
18 | ||
60c5eb7d XL |
19 | This will compile, because it has the constraint on the type parameter: |
20 | ||
21 | ``` | |
22 | struct Foo<T: 'static> { | |
23 | foo: &'static T | |
24 | } | |
25 | ``` |