简体   繁体   English

无法移出位于共享引用移动后面的 `*handle`,因为 `*handle` 具有类型

[英]cannot move out of `*handle` which is behind a shared reference move occurs because `*handle` has type

I want to use foreach to wait for thread termination.我想使用foreach来等待线程终止。 However, the following error occurs and it is not realized.但是,出现以下错误,并没有实现。 Please tell me.请告诉我。

cannot move out of `*handle` which is behind a shared reference  move occurs because `*handle` has type `std::thread::JoinHandle<()>`, which does not implement the `Copy` trait
let mut handles = Vec::new();
handles.push(thread::spawn(move || {
   let mut data = snd_rx.recv().unwrap();
   data += 1;
   let _ = rcv_tx.send(data);
}));

handles.iter().for_each(|handle| {
   let _ = handle.join(); // <- Error occurred
});

As already stated in a comment, the solution is to use into_iter instead of iter .正如评论中所述,解决方案是使用into_iter而不是iter Here's why:原因如下:

When you call the .iter() method, you can iterate over the contents of handles and get a shared reference into the vector (that means, if handles is of type Vec<T> , the argument in the closure is of type &T ).当您调用.iter()方法时,您可以遍历handles的内容并获取向量中的共享引用(这意味着,如果handles的类型为Vec<T> ,则闭包中的参数的类型为&T ) . Therefore, you cannot modify the content of the vector.因此,您不能修改矢量的内容。

But this is exactly what you are doing when assigning handle to _ (which just drops the assigned value immediately), since this assignment is moving the value out of the vector into the local variable.但这正是您在将handle分配给_ (它会立即删除分配的值)时所做的事情,因为此分配将值从向量中移到局部变量中。 The error message is saying, that that would be okay, if JoinHandle was to implement Copy (like integers do for example, these values are not moved but copied on assignment), but it does not.错误消息是说,如果JoinHandle要实现Copy (例如,像整数那样,这些值不会移动而是在赋值时复制),那没关系,但事实并非如此。

In contrast to that, when calling into_iter , you get an iterator, that consumes the vector (the vector moves into the iterator, which is then consumed when iterating over it).与此相反,当调用into_iter ,你会得到一个迭代器,它消耗向量(向量移动到迭代器中,然后在迭代它时被消耗)。 On every iteration, you get one element of the vector, but you also take ownership of that value, which is okay, since the vector is invalidated after iteration.在每次迭代中,您都会获得向量的一个元素,但您也拥有该值的所有权,这是可以的,因为向量在迭代后无效。 As you now have ownership of handle , you can move it into _ and drop the value.由于您现在拥有handle所有权,您可以将其移入_并删除该值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于 `slice[_]` 的类型为 `T`,它没有实现 `Copy` 特征,因此无法移出此处发生移动 - Cannot move out of here move occurs because `slice[_]` has type `T`, which does not implement the `Copy` trait 展开时不能移出共享引用后面的值 - Cannot move out of value which is behind a shared reference when unwrapping 无法移出共享引用后面的 `*h` - cannot move out of `*h` which is behind a shared reference 无法移出共享引用后面的 *** (Clap) - cannot move out of *** which is behind a shared reference (Clap) Rust hashmap inside Struct error: cannot move out of xxx which is behind a shared reference - Rust hashmap inside Struct error: cannot move out of xxx which is behind a shared reference 使用 Box 时无法移出共享引用后面的 `*X` - Cannot move out of `*X` which is behind a shared reference when using Box 无法移出借用的内容/不能移出共享引用的后面 - Cannot move out of borrowed content / cannot move out of behind a shared reference 无法移出共享引用后面的 `config.filename`。 我不知道为什么我收到这个错误 - Cannot move out of `config.filename` which is behind a shared reference. I don't know why I got this error 移动发生是因为值的类型为 Vec<T> ,它没有实现 `Copy` 特性 - move occurs because value has type Vec<T>, which does not implement the `Copy` trait 无法移出递归结构的共享引用 - Cannot move out of shared reference of a recursive struct
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM