I don't understand in Rust what is happening with a struct in a struct when we Box
the parent struct.
struct Outer1 {
child: Inner1,
}
struct Inner1 {
n: i32,
}
struct Outer2 {
child: Box<Inner2>,
}
struct Inner2 {
n: Box<i32>,
}
pub fn main() {
let x1 = Box::new(Outer1 {
child: Inner1 { n: 1 },
});
let x2 = Box::new(Outer2 {
child: Box::new(Inner2 { n: Box::new(1) }),
});
}
x2.child
and x2.child.n
should be on the heap, right? Where is x1.child
and x1.child.n
: the stack or the heap?
If child.n
would be of type String
, n
should be a reference and String
needs no Box
to be on the heap? Is this correct?