简体   繁体   English

如何克隆 Vec <Box<dyn SomeTrait> &gt; 在一个物体上?

[英]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.

相关问题 如何克隆 Vec <Box<dyn Trait> &gt;? - How can I clone a Vec<Box<dyn Trait>>? 盒状容器 FixedSizeBox&lt;128, dyn SomeTrait&gt; 可以从实现 SomeTrait 的类型构造 - Box like container FixedSizeBox<128, dyn SomeTrait> which can be constructed from type implementing SomeTrait Result&lt;(), Box&lt;(dyn SomeTrait + &#39;static)&gt;&gt; 不满足 Trait Bound - Trait Bound is not satisfied for Result<(), Box<(dyn SomeTrait + 'static)>> 如何移动 Vec <box<dyn trait> &gt; 走进 Vec <rc<refcell<dyn trait> &gt;&gt; </rc<refcell<dyn></box<dyn> - How to move a Vec<Box<dyn Trait>> Into Vec<Rc<RefCell<dyn Trait>>> 如何使用 Arc 进行运行时多态性<Mutex<dyn SomeTrait> &gt; 在 Rust 中? - How to do runtime polymorphism using Arc<Mutex<dyn SomeTrait>> in Rust? 如何克隆盒装特征错误(框<dyn error> )?</dyn> - How to clone a boxed trait Error ( Box<dyn Error> )? 被 Vec 迷惑了<Box<dyn T> &gt; 和 TypeId 行为 - Bewildered by Vec<Box<dyn T>> and TypeId behaviour Rust:无法收集“迭代器” <item = box<dog> &gt;` 到 `Vec <box<dyn animal> &gt;` </box<dyn></item> - Rust: can't collect an `Iterator<Item = Box<Dog>>` into `Vec<Box<dyn Animal>>` 无法克隆Vec <Box<Trait> &gt;因为Trait不能成为一个对象 - Can't clone Vec<Box<Trait>> because Trait cannot be made into an object 我怎样才能制作`Box<dyn iterator> ` 可窥视并避免终身错误?</dyn> - How can I make `Box<dyn Iterator>` peekable and avoid lifetime errors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM