Closed
Description
This code does not compile (playground):
extern crate futures_util; // 0.3.4
use futures_util::stream::StreamExt;
struct Foo;
impl Foo {
fn foo(&mut self) { }
}
fn main() {
let mut x = Foo;
futures_util::stream::iter(0..1).for_each(move |_| {
async { x.foo(); }
});
}
due to:
error: captured variable cannot escape `FnMut` closure body
--> src/main.rs:12:9
|
11 | futures_util::stream::iter(0..1).for_each(move |_| {
| - inferred to be a `FnMut` closure
12 | async { x.foo(); }
| ^^^^^^^^^^^^^^^^^^ returns a reference to a captured variable which escapes the closure body
|
= note: `FnMut` closures only have access to their captured variables while they are executing...
= note: ...therefore, they cannot allow references to captured variables to escape
It would be helpful if the compiler said which captured variable escaped the closure (in the example, it is x
, but this is not always clear if there are many captured variables).
Metadata
Metadata
Assignees
Labels
Area: Async & AwaitArea: Closures (`|…| { … }`)Area: Messages for errors, warnings, and lintsAsync-await issues that have been triaged during a working group meeting.Category: An issue proposing an enhancement or a PR with one.Diagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint; hard to understand for new users.Medium priorityRelevant to the compiler team, which will review and decide on the PR/issue.