rustfunction-call

How does Rust return large types (before optimisation)?


With a function signature such as:

pub fn hash(input: &[u8]) -> [u8; 32]

how do those 32 bytes get passed to the calling function? (Before optimisation, which might place some parts in registers.)

Possibilities include:

Just curious, I have no need for this information. The answer may differ by architectures.


Solution

  • There is neither a copy nor a heap allocation involved.

    The caller function allocates space for the value on its stack. The callee takes an additional hidden parameter, the address of the return value. It writes the value there. The caller passes the address of the allocated space on its stack as the value for the hidden parameter.

    Of course, none of that is guaranteed. This is just how it happens to work right now.