Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an enum containing a Box not copyable?

Tags:

rust

Boxes and arrays are copyable, so why does this not compile?

#[derive(Debug, Copy, Clone)]
enum Octree{
    Branch(Box<[Octree; 8]>),
    Filled,
    Empty,
}

Compile error:

main.rs:3:17: 3:21 error: the trait `Copy` may not be implemented for this type; variant `Branch` does not implement `Copy` [E0205]

EDIT: Ok, so I don't want Octree to be copyable after all. But how do I make it mutable? I want to be able to change children of a node.

like image 443
Joonazan Avatar asked Nov 27 '25 02:11

Joonazan


1 Answers

Copy is only for types that are trivially copyable. Box is not Copy because merely copying the pointer would violate the single ownership principle.

You want to use Clone and its clone method here.

like image 126
A.B. Avatar answered Nov 29 '25 22:11

A.B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!