How to clone vector element (trait object) via iterator reference

I am ending up with an error message "type list:Item<'_> cannot be dereferenced" when I try to clone the trait object Item via the vector Vec<Item> iterator next() method. The iterator next() returns Option<&Item> in the while loop, that unwraps to provide just &Item.

I believe the solution to dereference the reference to Item would make use of the Deref trait, after doing plenty searches yet was unable to find an example of dereferencing a reference to a trait object, in order for me to clone the vector element.

I do really want to clone the entire trait object element of the vector to another trait object, that will contain the memory copy of the vector element. Thus there will be two copies of the trait object in memory, yet located in two different structs.

The question is not really about iterators - trait objects can't be cloned at all without extra steps like using dyn_clone. Not sure whether it allows to go from &dyn Trait to Box<dyn Trait>, however.

1 Like

Thanks for this helpful crate. Solved my issue.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.