Recursive types ok in Vec but not Arrays?

How come the following is ok:

struct My {
    a: Vec<My>,

But not this:

struct My2 {
    a: [My2; 10],

I understand that My2 does not have a know size and therefore I need to wrap inside a Box like so:

struct My3 {
    a: [Box<My3>; 10],

But should not the same problem exist for Vec and needing me to use:

struct My {
    a: Vec<Box<My>>,

That’s because the Vec itself is like a resizeable Box. The Vec object has a fixed size of 3x usize, and the content is stored on the heap.

You can also use Box<[My; 10]> (it’s one box of 10 structs, your box example is one struct of 10 boxes).


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.