[英]How can I clone a Vec<Box<dyn SomeTrait>> on an object?
I have the following trait definition representing a sub-block within a DSL:我有以下特征定义表示 DSL 中的子块:
pub trait SubBlock {
fn get_command(&self) -> Command;
fn get_parent_id(&self) -> u32;
}
Here这里
.map(|bsb| {
*bsb
})
you trigger a Copy
not a Clone
.您触发
Copy
而不是Clone
。
To be able to .clone()
values, you need to know, that they implement the Clone
trait.为了能够
.clone()
值,您需要知道它们实现了Clone
特征。
So make it所以做吧
pub children: Option<Vec<Box<dyn SubBlock + Clone>>>
and implement Clone
for all the things, that might end up in that vector and then use .clone()
.并为所有可能最终出现在该向量中的事物实现
Clone
,然后使用.clone()
。
The rest should become easier after that.在那之后,其余的应该会变得更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.