Cloning a Vec of generics?

Still trying to use generics in apparently ways they don't want to be used.

struct Node<T,U>{
    children:Vec<Node<T,U>>
}
impl<T,U> Node<T,U>{
    fn clone(&self)->Self{
        Node{
            children: self.clone_children(&self.children),
        }
    fn clone_children(&self, list: &Vec<Node<T,U>>)->Vec<Node<T,U>>{
        *list.clone()
    }
}

But I get the error: cannot move out of a shared reference, if I remove the *, I obviously get mismatched types because it's cloning a reference. If I do (*list).clone() I get the error:

the method `clone` exists for struct `Vec<Node<T, U>>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`Node<T, U>: Clone`
which is required by `Vec<Node<T, U>>: Clone`

So, I'm not quite sure where to go from here? Suggestions?

You are almost there with (*list).clone(), but for a Vec to be Clone all its elements also need to be Clone. The issue here isn't strictly with the generics, but with the fact that it doesn't know that a Node is Clone. I would probably go for implementing Clone all in one go, as in this playground. I've added PhantomData, since otherwise the T and U parameters are unused and I'm not sure quite what you want to do with them. Depending on what you want to do with T and U, they may also need a Clone bound on them. Once you have Clone for Node<T,U>, a clone_children() should work with (*list).clone() (playground with clone_children()).

Apparently I just can't read. It appears the problem was that yes, I had a method; what I didn't have was the trait for clone.

Implementing the trait solved the error

impl<T,U> Clone for Node<T,U>{

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.