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

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

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;
        }
    }
}

但它给出了以下错误:

   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
  |             |
  |             help: consider adding an explicit lifetime bound...: `T: 'a +`

error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.

我根本不明白这个错误。添加生命周期界限并不能解决问题 ( Playground ),除非添加的生命周期界限是'static( Playground )。

我试着更换BTreeSetVecVecDequeLinkedListHashSetBinaryHeap。全部编译没有错误。有什么特别之处BTreeSet

以上是异步迭代BTreeSet时出现奇怪的生命周期错误的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>