Async closure vs closure

I am writing async eval loop with async handler inside. But attempt to move a value inside the handler is problematic. If I change async closure to plain closure, which returns future::ready then it compiles.
The difference is in lines 12 and 21.
What’s the difference and what is the problem?

#![feature(async_await, await_macro, futures_api)]

use futures::stream::{self, StreamExt};
use std::collections::HashSet;

fn main() {
    // Tis errors with:
    // error[E0507]: cannot move out of captured variable in an `FnMut` closure
    let mut h = HashSet::new();
    let _exec_loop = async move {
        let s = stream::iter(1..=3);
        await!(s.for_each(async move|i|{
            h.insert(i);
        }));
    };

    // This compiles
    let mut h = HashSet::new();
    let _exec_loop = async move {
        let s = stream::iter(1..=3);
        await!(s.for_each(move |i| {
            h.insert(i);
            futures::future::ready(())
        }));
    };
}
error[E0507]: cannot move out of captured variable in an `FnMut` closure
  --> src/main.rs:12:40
   |
9  |       let mut h = HashSet::new();
   |           ----- captured outer variable
...
12 |           await!(s.for_each(async move|i|{
   |  ________________________________________^
13 | |             h.insert(i);
14 | |         }));
   | |_________^ cannot move out of captured variable in an `FnMut` closure
1 Like

Hi @vchekan, have you figured out what the problem is and how to solve it ?