简体   繁体   English

Rust:无法实现结构的复制特征

[英]Rust: Can not implement copy trait for struct

We have the following situation:我们有以下情况:

#[derive(Debug, Clone, PartialEq)]
pub struct Bar{
pub name: String,
pub anotherbar: Box<Option<Bar>>,
pub customenum: Option<Vec<Enum>>,
pub yesno: bool,
}

#[derive(Debug, Clone, Copy)]
pub struct foo {
pub name: String,
pub vector: Vec<bar>,
pub tuple: Vec<(u8, Bar)>,
}

and get the error message:并得到错误信息:

error[E0204]: the trait `Copy` may not be implemented for this type
   --> src/types.rs:459:24
    |
459 | #[derive(Debug, Clone, Copy)]
    |                        ^^^^
460 | pub struct Deck{
461 |     pub name: String,
    |     ---------------- this field does not implement `Copy`
462 |     pub commander: Vec<Card>,
    |     ------------------------ this field does not implement `Copy`
463 |     pub library: Vec<(u8, Card)>,
    |     ---------------------------- this field does not implement `Copy`
    |
    = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0204`.

I want to use the struct bar in a parallelization situation, where multiple requests add to the tuple field.我想在并行化情况下使用 struct bar,其中多个请求添加到元组字段。

Can anyone help and explain?谁能帮忙解释一下?

Thanks and happy holidays.谢谢,节日快乐。

Copy indicates "Types whose values can be duplicated simply by copying bits." Copy表示“可以简单地通过复制位来复制其值的类型”。 (As a practical matter, Copy also strongly implies that copying the type is very cheap and so can be done implicitly.) Vec and String cannot be copied this way due to their internal buffers. (实际上, Copy还强烈暗示复制类型非常便宜,因此可以隐式完成。) VecString由于它们的内部缓冲区,不能以这种方式复制。 They are both Clone (in the case of Vec , only when its contents are also Clone ).它们都是Clone (在Vec的情况下,只有当它的内容也是Clone时)。 Clone means that a copy is possible, but must be done explicitly using the .clone() call. Clone意味着可以进行复制,但必须使用.clone()调用显式完成。

I want to use the struct bar in a parallelization situation, where multiple requests add to the tuple field.我想在并行化情况下使用 struct bar,其中多个请求添加到元组字段。

This sounds like you're trying to create shared mutable state.这听起来像您正在尝试创建共享可变 state。 You should first carefully consider if this is what you really want.您应该首先仔细考虑这是否是您真正想要的。 Typically you would instead do work in parallel and then collect all the data together at the end.通常,您会改为并行工作,然后在最后收集所有数据。 That way the parallel activities don't interfere with each other.这样并行活动就不会相互干扰。 But if they must work together, then you'll need to use Mutex and other tools to provide thread-safety.但是如果它们必须一起工作,那么您将需要使用 Mutex 和其他工具来提供线程安全。

In either case, Copy wouldn't help you there, since that would create independent copies of the struct, which seems the opposite of what you're describing.在任何一种情况下, Copy都不会帮助您,因为这会创建结构的独立副本,这似乎与您所描述的相反。

For more on Copy , see the docs explaining it.有关Copy的更多信息,请参阅解释它的文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM