E0308 rust type_error ai_generated true

error[E0308]: mismatched types -- expected `impl Future<Output = ()>`, found `()`

ID: rust/e0308-mismatched-types-async

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2023-08-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.65.0 active
1.72.0 active
1.80.0 active

Root Cause

An async function or block is returning a value directly instead of returning a Future, often due to missing 'async' keyword or incorrect return type.

generic

中文

异步函数或块直接返回了一个值,而不是返回一个 Future,通常是由于缺少 'async' 关键字或返回类型不正确。

Official Documentation

https://doc.rust-lang.org/stable/error_codes/E0308.html

Workarounds

  1. 95% success Add 'async' keyword to the function definition: 'async fn my_func() -> () { /* ... */ }'. This wraps the return value in a Future.
    Add 'async' keyword to the function definition: 'async fn my_func() -> () { /* ... */ }'. This wraps the return value in a Future.
  2. 85% success If the function must return a Future manually, change return type to 'Pin<Box<dyn Future<Output = ()>>>' and return 'Box::pin(async move { ... })'.
    If the function must return a Future manually, change return type to 'Pin<Box<dyn Future<Output = ()>>>' and return 'Box::pin(async move { ... })'.

中文步骤

  1. Add 'async' keyword to the function definition: 'async fn my_func() -> () { /* ... */ }'. This wraps the return value in a Future.
  2. If the function must return a Future manually, change return type to 'Pin<Box<dyn Future<Output = ()>>>' and return 'Box::pin(async move { ... })'.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Adding '.await' at the call site without making the calling function async — this causes a different error about 'await' outside async context.

  2. 60% fail

    Returning a 'Box::pin(future)' without adjusting the return type — still mismatched type.