I was trying to understand how structs behave when returned from methods. There is a section in the nightlies section of the "Rust Book" that said if you used the syntax...
let x = box i_return_a_struct();
.. that there wouldn't be a copy, therefore there is no need to return a pointer. But when I started playing with it, it appears that the box
is not needed, unless you need the value to exist on the heap.
#[derive(Debug)]
struct Dummy {
data: i64,
}
impl Drop for Dummy {
fn drop(&mut self) {
println!("{:?} is going out of scope", self as *const Dummy);
}
}
fn make_dummy(i: i64) -> Dummy {
Dummy { data: i }
}
fn main() {
{
let i = 15i32;
println!("{:?} is a variable on the stack frame", &i as *const i32);
let dummy1 = make_dummy(1);
println!("{:?} was returned and is being used", &dummy1 as *const Dummy);
let dummy2 = make_dummy(2);
println!("{:?} was returned and is being used", &dummy2 as *const Dummy);
let dummy3 = Box::new(make_dummy(3));
println!("{:?} box was returned and is being used", &(*dummy3) as *const Dummy);
let dummy4 = Box::new(make_dummy(4));
println!("{:?} box was returned and is being used", &(*dummy4) as *const Dummy);
}
println!("Leaving main");
}
Output:
0x23fb94 is a variable on the stack frame
0x23faf8 was returned and is being used
0x23fa50 was returned and is being used
0x2825030 box was returned and is being used
0x2825040 box was returned and is being used
0x2825040 is going out of scope
0x2825030 is going out of scope
0x23fa50 is going out of scope
0x23faf8 is going out of scope
Leaving main
Do values/structs in return position always get allocated in the parents stack frame or receiving box?
EDIT: PS - is there any guidance in the docs for as to when copy elision will occur in general?
EDIT: Beyond the accepted solution, the following Q+A was enlightening: What are move semantics exactly? Clarified many points for me.