Understanding Rust Error E0708: Async Non-Move Closures with Parameters
Rust Error E0708 Explained
Error E0708 in Rust occurs when you try to use an async non-move closure with parameters. Currently, async non-move closures with parameters are not supported, and you'll need to use an async move closure instead.
The error often appears in the following erroneous code example:
#![feature(async_closure)]
fn main() {
let add_one = async |num: u8| { // error!
num + 1
};
}
To resolve Rust Error E0708, use an async move closure with the 'move' keyword, like so:
#![feature(async_closure)]
fn main() {
let add_one = async move |num: u8| { // ok!
num + 1
};
}
Difference between Async Non-Move and Async Move Closures
Async non-move closures allow you to capture the environment by reference, while async move closures capture the environment by value, which means it takes ownership of any captured variables.
Using async move closures is necessary in some cases, such as when you need to pass the closure through different threads or to ensure that the lifetime of captured variables is properly extended.
Additionally, async move closures can prevent borrowing issues that may arise when using async non-move closures with parameters.