How to implement Copy trait for Box

You can't implement Copy for Box, that would allow creation of multiple boxes referencing the same thing. The good news is that you don't need to in order to solve your problem.

The issue here is that you have a reference to the node inside the Some and you are trying to move the value out of it, which leaves a "gap" that invalidates self, which can quickly become unsafe. The solution is to replace the value in first_node with something to fill the gap, and since it is an Option, you can just use None. This is precisely what Option::take() provides for, so try matching on first_node.take() instead of first_node.

4 Likes