Joonazan
Joonazan

Reputation: 1416

Why is an enum containing a Box not copyable?

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.

Upvotes: 3

Views: 623

Answers (1)

A.B.
A.B.

Reputation: 16630

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.

Upvotes: 7

Related Questions