Rust function calling conventions


I have a few questions regarding how the stack is manipulated for Rust function calls.
The Rust book suggests that the arguments are passed from left to right on the stack with the stack growing upwards. ( )

This confuses me a little bit as I always thought that the stack order, how arguments are passed adhered to some calling convention. I was wondering what is the exact convention that Rust uses for its function calls? Because it doesn’t look like cdecl so I am not sure what exactly it is. I have looked online but I haven’t found anything concrete regarding this.

I know Rust allows the use of different calling conventions so I was wondering how that is incorporated as well.

Any help is appreciated in this matter. Thanks :slight_smile:


I am by no means an expert, but I am not sure if any particular convention is defined for Rust-to-Rust calls.

The Rust compiler wants to create optimal code. It could decide to inline all copies of a function, in which case there is no call at all. It could use multiple valid conventions that pass things in registers to make the code run faster. It could also use things like stack redzones which confuse analyzing the stack even further.

If you need to know what a function’s calling convention is, mark it as a C exportable function:

pub extern fn foo() {
    // ...

Then it should match the calling convention of your target’s C compiler.


The rust ABI/calling convention is currently intentionally undefined, as is explained in the FAQ

In particular, they’re still experimenting with different designs, and there are several things Rust can do that do not translate well to the C convention.

This RFC deals with the topic. One nice remark there is to

Note that C++ still doesn’t have a stable ABI, and it took C decades to get one.

What many people also don’t know is that extern is short for extern "C". Rust has infrastructure in place to support multiple calling conventions, the more well-known ones are “C”, “naked” and “Rust”.