Rust 如何使用销而不是圆弧通过Vec<;u8>;通过引用异步块?

Rust 如何使用销而不是圆弧通过Vec<;u8>;通过引用异步块?,rust,async-await,Rust,Async Await,我想使用弧对Vec执行多次操作: use futures::{ executor::{block_on, ThreadPool}, task::SpawnExt, }; // 0.3.4 use std::{pin::*, sync::Arc}; fn foo(b: Arc<Vec<u8>>) { println!("{:?}", b); } #[test] fn pin_test() { let v = Arc::new(vec![1

我想使用
弧对
Vec
执行多次操作:

use futures::{
    executor::{block_on, ThreadPool},
    task::SpawnExt,
}; // 0.3.4
use std::{pin::*, sync::Arc};

fn foo(b: Arc<Vec<u8>>) {
    println!("{:?}", b);
}

#[test]
fn pin_test() {
    let v = Arc::new(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
    let mut pool = ThreadPool::new().unwrap();
    for _ in 0..10 {
        let v1 = v.clone();
        let handle = pool
            .spawn_with_handle(async {
                foo(v1);
            })
            .unwrap();
        block_on(handle);
    }
}
这会产生以下错误:

错误[E0597]:`v1`寿命不够长
-->src/lib.rs:19:23
|
18 |。使用_句柄生成_(异步{
|   ________________________________-_____-
|  |________________________________|
| ||
19 | | | foo(&*v1);
|| | ^借来的价值活得不够长
20 | ||             })
| ||             -
| ||_____________|
||uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
|参数要求为“静态”借用“v1”`
...
23 |        }
|-`v1`在还借来的时候掉在这里了
我知道
Pin
应该将
Vec
数据固定到一个特定点,这样所有调用都可以引用相同的数据。使用
Pin
传递对
foo()的引用的正确方法是什么


我使用的是Rust 1.39。

您缺少一个
移动

改变

let handle = pool
    .spawn_with_handle(async {
        foo(&*v1);
    })
    .unwrap();


我很困惑这里到底发生了什么,是否有人在使用Pin码。无论哪种情况,您都在进行克隆,然后对克隆的值进行操作。将克隆的值移动到异步块中应该可以工作,而不考虑任何管脚。此外,考虑到您的Arc、Pin和Vec都实现了克隆,我不清楚在每个场景中都克隆了什么……是的,您是对的。您有正确的解决方案吗?这不会通过引用异步块来传递
Vec
。它故意将
Vec
的所有权转移给块。
let handle = pool
    .spawn_with_handle(async {
        foo(&*v1);
    })
    .unwrap();
let handle = pool
    .spawn_with_handle(async move {
        //                   ^^^^
        foo(&*v1);
    })
    .unwrap();