Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

这是一段伪代码

async fn a() {
    let a:T = ...;//T一个没有实现 Send的类型
    let b = foo(a);
    boo(b).await;
}

async fn boo() -> i32 {
    ...
}

这种情况下,会有一个future cannot be sent between threads safely的错误。
解决方法是,保证aawait之前drop

async fn a() {
    let b;
    {
        let a:T = ...;//T一个没有实现 Send的类型
        b = foo(a);//这里提取的是可以Send的数据
    }
    boo(b).await;
}

不过手动drop是不行的

async fn a() {
    let a:T = ...;//T一个没有实现 Send的类型
    let b = foo(a);//这里提取的是可以Send的数据
    drop(a);//编译器不认
    boo(b).await;
}

更进一步的解释可以参考https://blog.csdn.net/wangjun861205/article/details/118084436

评论