Open tjallingt opened 10 months ago
works uses a regular closure (|_| async { ... }) that borrows client and base_url by reference.
error uses a move closure (move |_| async { ... }) that takes ownership of these variables, leading to the error you encountered.
*Remove move Keyword: In error, remove move to allow borrowing by reference instead of ownership transfer: Rust
async fn error(client: &Client, baseurl: &str) { // ... let : Vec<_> = stream::iter(&[1]) .map(|_| async { / use client and base_url by reference / }) .collect() .await; }
@rustbot label +D-confusing +D-newcomer-roadblock -needs-triage
Code
Current output
Desired output
No response
Rationale and extra context
While programming I copy pasted my own code containing a move closure. I then encountered this error and attempted to figure out what was going wrong in this code.
My attempts to fix the error involved changing the type of
base_url
, cloningbase_url
and moving around the creation ofurl
. I did not consider that themove
may have been the cause of this error, perhaps because my mental model of whatmove
is doing exactly is not quite complete.If one writes a closure without
move
and runs into issues where it is necessary, the compiler helpfully suggests adding it. In this case it would be nice if the compiler could check that the move is not necessary (and in fact is preventing the code from compiling) and then report a suggestion to remove it.As it stands the error seems to suggest that the closure being
FnMut
is the cause of the error.Other cases
No response
Rust Version
Anything else?
No response