拼接方法类型不匹配

splice method type mismatch

所以我有一个布尔向量隐藏在互斥量的弧后面:

let mut vec: Arc<Mutex<Vec<bool>>> = Arc::new(Mutex::new(vec![false; size]));

然后我产生多个线程,这些线程在另一个向量(相同大小)的段上工作。例如,假设 size=20。然后,线程 1 处理索引 0 - 4,线程 2 处理索引 5 - 9,依此类推。

每个线程在段上完成工作后,我想把它放回这个 vec Arc/Mutex/Vector。我这样做有点麻烦。这是我到目前为止尝试过的:

let mut vec = next.lock().unwrap(); // Blocks until we can acquire
let mut data = *(& *vec); // Pulls the vector out (type std::vec::Vec<bool>)
data.splice(min..=max, segment.iter().clone()).collect(); // Tries to add our segment in

segment(见最后一行)是在线程中创建的std::vec::Vec<bool>

希望我的意图是明确的。

但是,我收到了这个错误:

type mismatch resolving `<std::slice::Iter<'_, bool> as IntoIterator>::Item == bool`

data.splice(min..=max, segment.iter().clone()).collect();
     ^^^^^^ expected reference, found `bool`

note: expected reference `&bool`
found type `bool`

有什么想法吗?

您不小心用 .clone(). You probably meant to call .cloned() 克隆了迭代器,因此迭代器克隆了它的项。

data.splice(min..=max, segment.iter().cloned()).collect();