-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add long explanation for E0726 #87655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4336f9d
02af45c
2a3b555
adcb9b9
58fdba8
bfdaf67
6fe2ef5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
An argument lifetime was elided in an async function. | ||
|
||
When a struct or a type is bound/declared with a lifetime it is important for | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write a short summary of the error here, the explanation goes below. :) Something like "An argument's lifetime was elided in an async function". |
||
the Rust compiler to know, on usage, the lifespan of the type. When the | ||
lifetime is not explicitly mentioned and the Rust Compiler cannot determine | ||
the lifetime of your type the following error occurs. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0726 | ||
use futures::executor::block_on; | ||
|
||
struct Content<'a> { | ||
title: &'a str, | ||
body: &'a str, | ||
} | ||
|
||
async fn create(content: Content) { // error: implicit elided | ||
// lifetime not allowed here | ||
println!("title: {}", content.title); | ||
println!("body: {}", content.body); | ||
} | ||
|
||
let content = Content{ title: "Rust", body: "is great!" }; | ||
let future = create(content); | ||
block_on(future); | ||
``` | ||
|
||
Specify desired lifetime of parameter `content` or indicate the anonymous | ||
lifetime like `content: Content<'_>`. The anonymous lifetime tells the Rust | ||
compiler that `content` is only needed until create function is done with | ||
it's execution. | ||
|
||
The `implicit elision` meaning the omission of suggested lifetime that is | ||
`pub async fn create<'a>(content: Content<'a>) {}` is not allowed here as | ||
lifetime of the `content` can differ from current context. | ||
|
||
Know more about lifetime elision in this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a working version of your example above. |
||
[chapter][lifetime-elision] and a chapter on lifetimes can be found | ||
[here][lifetimes]. | ||
|
||
[lifetime-elision]: https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html#lifetime-elision | ||
[lifetimes]: https://doc.rust-lang.org/rust-by-example/scope/lifetime.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to remove E0725 from the list of error codes from below (it's not supposed to be declared twice in this file).