Created
July 7, 2017 17:06
-
-
Save nikomatsakis/ca5c028851749bd4e871cefbdd2a4e4e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This error code indicates a mismatch between the lifetimes appearing in the | |
function signature (i.e., the parameter types and the return type) and the | |
data-flow found in the function body. | |
Erroneous code example: | |
```compile_fail,E0621 | |
fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 { // explicit lifetime required | |
// in the type of `y` | |
if x > y { x } else { y } | |
} | |
``` | |
In the code above, the function is returning data borrowed from either `x` or `y`, | |
but the `'a` annotation indicates that it is returning data only from `x`. To fix the | |
error, the signature and the body must be made to match. Typically, this is done | |
by updating the function signature. So, in this case, we change the type of `y` to | |
`&'a i32`, like so: | |
``` | |
fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 { | |
if x > y { x } else { y } | |
} | |
``` | |
Now the signature indicates that the function data borrowed from either `x` or `y`. | |
Alternatively, you could change the body not to return data from y: | |
``` | |
fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32 { | |
x | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment