小编ale*_*pha的帖子

异步迭代 BTreeSet 时出现奇怪的生命周期错误

我希望以下代码中的异步块实现SendPlayground):

use std::collections::BTreeSet;
use std::future::ready;

pub fn test<T: Sync>(set: &BTreeSet<T>) -> impl Send + '_ {
    async move {
        for _ in set {
            ready(()).await;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但它给出了以下错误:

   Compiling playground v0.0.1 (/playground)
error[E0311]: the parameter type `T` may not live long enough
 --> src/lib.rs:4:44
  |
4 | pub fn test<T: Sync>(set: &BTreeSet<T>) -> impl Send + '_ {
  |             --                             ^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
  |             |
  | …
Run Code Online (Sandbox Code Playgroud)

generics lifetime rust async-await

18
推荐指数
1
解决办法
236
查看次数

标签 统计

async-await ×1

generics ×1

lifetime ×1

rust ×1