]>
Commit | Line | Data |
---|---|---|
d9bb1a4e FG |
1 | A lifetime cannot be determined in the given situation. |
2 | ||
3 | Erroneous code example: | |
4 | ||
5 | ```compile_fail,E0495 | |
6 | fn transmute_lifetime<'a, 'b, T>(t: &'a (T,)) -> &'b T { | |
7 | match (&t,) { // error! | |
8 | ((u,),) => u, | |
9 | } | |
10 | } | |
11 | ||
12 | let y = Box::new((42,)); | |
13 | let x = transmute_lifetime(&y); | |
14 | ``` | |
15 | ||
16 | In this code, you have two ways to solve this issue: | |
17 | 1. Enforce that `'a` lives at least as long as `'b`. | |
18 | 2. Use the same lifetime requirement for both input and output values. | |
19 | ||
20 | So for the first solution, you can do it by replacing `'a` with `'a: 'b`: | |
21 | ||
22 | ``` | |
23 | fn transmute_lifetime<'a: 'b, 'b, T>(t: &'a (T,)) -> &'b T { | |
24 | match (&t,) { // ok! | |
25 | ((u,),) => u, | |
26 | } | |
27 | } | |
28 | ``` | |
29 | ||
30 | In the second you can do it by simply removing `'b` so they both use `'a`: | |
31 | ||
32 | ``` | |
33 | fn transmute_lifetime<'a, T>(t: &'a (T,)) -> &'a T { | |
34 | match (&t,) { // ok! | |
35 | ((u,),) => u, | |
36 | } | |
37 | } | |
38 | ``` |