Does the variable s
in print_struct
refer to data on the heap or on the stack?
struct Structure {
x: f64,
y: u32,
/* Use a box, so that Structure isn't copy */
z: Box<char>,
}
fn main() {
let my_struct_boxed = Box::new(Structure {
x: 2.0,
y: 325,
z: Box::new('b'),
});
let my_struct_unboxed = *my_struct_boxed;
print_struct(my_struct_unboxed);
}
fn print_struct(s: Structure) {
println!("{} {} {}", s.x, s.y, s.z);
}
As I understand it, let my_struct_unboxed = *my_struct_boxed;
transfers the ownership away from the box, to my_struct_unboxed
, and then to s
in the function print_struct
.
What happens with the actual data? Initially it is copied from the stack onto the heap by calling Box::new(...)
, but is the data some how moved or copied back to the stack at some point? If so, how? And when is drop
called? When s
goes out of scope?