E0308 rust type_error ai_generated true

错误[E0308]: 类型不匹配 —— 期望 `impl Future<Output = ()>`,找到 `()`

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

ID: rust/e0308-mismatched-types-async

其他格式: JSON · Markdown 中文 · English
90%修复率
85%置信度
1证据数
2023-08-12首次发现

版本兼容性

版本状态引入弃用备注
1.65.0 active
1.72.0 active
1.80.0 active

根因分析

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

English

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

官方文档

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

解决方案

  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 { ... })'.

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 60% 失败

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