Who owns the return value?

fn main () {
    println! ("{}", some_function(x));
}

fn some_function (y) -> i32 {
    y+32
}

Where is the return value from some_function stored? Who owns that?

1 Like

Compiler automatically reserves temporary storage for things created in an expression. It's equivalent of:

{
let tmp = some_function(x);
println! ("{}", tmp);
}

And the values are stored on the stack.

4 Likes

If it helps, you can view the assembly that gets generated by using something like the playground's assembly output (click the "..." in the top-left next to "build" to change outputs), or Godbolt.

Looking at the LLVM IR and MIR forms can sometimes be useful. MIR is the closest form to how the language is meant to work and contains information like scopes and when a variable is alive or dead (StorageLive and StorageDead), but interpreting the output requires a decent understanding of the language and compiler internals...

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