]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0621.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0621.md
1 This error code indicates a mismatch between the lifetimes appearing in the
2 function signature (i.e., the parameter types and the return type) and the
3 data-flow found in the function body.
4
5 Erroneous code example:
6
7 ```compile_fail,E0621
8 fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 { // error: explicit lifetime
9                                              //        required in the type of
10                                              //        `y`
11     if x > y { x } else { y }
12 }
13 ```
14
15 In the code above, the function is returning data borrowed from either `x` or
16 `y`, but the `'a` annotation indicates that it is returning data only from `x`.
17 To fix the error, the signature and the body must be made to match. Typically,
18 this is done by updating the function signature. So, in this case, we change
19 the type of `y` to `&'a i32`, like so:
20
21 ```
22 fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 {
23     if x > y { x } else { y }
24 }
25 ```
26
27 Now the signature indicates that the function data borrowed from either `x` or
28 `y`. Alternatively, you could change the body to not return data from `y`:
29
30 ```
31 fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 {
32     x
33 }
34 ```