Skip to content

async closure does not implement FnMut because it captures state from its environment for async move closures capturing copyable values, while closures returning async move blocks work fine #140403

Open
@ProbstDJakob

Description

@ProbstDJakob

async move closures do not implement FnMut (and therefore also not Fn) when capturing a copyable value, but closures returning a async move block do.

I tried this code:

fn main() {}

fn needs_fn_mut<T, F: FnMut() -> T>(mut f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn_mut(async move || { copyable })
}
Same with Fn

fn main() {}

fn needs_fn<T, F: Fn() -> T>(f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn(async move || { copyable })
}

Working code with async move blocks:

fn main() {}

fn needs_fn_mut<T, F: FnMut() -> T>(mut f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn_mut(|| async move { copyable })
}
Same with Fn

fn main() {}

fn needs_fn<T, F: Fn() -> T>(f: F) -> T {
    (f)()
}

fn hello() -> impl Future<Output = u32> {
    let copyable = 0;

    needs_fn(|| async move { copyable })
}

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
Backtrace

error: async closure does not implement `FnMut` because it captures state from its environment
  --> <anon>:10:18
   |
10 |     needs_fn_mut(async move || { copyable })
   |     ------------ ^^^^^^^^^^^^^
   |     |
   |     required by a bound introduced by this call
   |
note: required by a bound in `needs_fn_mut`
  --> <anon>:3:23
   |
3  | fn needs_fn_mut<T, F: FnMut() -> T>(mut f: F) -> T {
   |                       ^^^^^^^^^^^^ required by this bound in `needs_fn_mut`

error: aborting due to 1 previous error

This issue may be linked to #125247.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-closures`async || {}`C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions