> For the complete documentation index, see [llms.txt](https://docs.hex-rays.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hex-rays.com/core/types/concepts/rust-calling-convention.md). Agents can also search and read this documentation through its [MCP server](https://docs.hex-rays.com/~gitbook/mcp).

# Rust Calling Convention

{% hint style="warning" %}
Rust doesn't guarantee a stable ABI, so the rules on this page may change with new compiler versions. They come from analyzing binaries produced by rustc, not from an official specification, so we can't guarantee they're complete, accurate or up to date, even for the rustc versions we analyzed most thoroughly.
{% endhint %}

The default ABI of the Rust compiler (rustc), `extern "Rust"`, is not the platform's C convention. IDA models it as `__rust` (`CM_CC_RUST`). It uses the same registers as C and distributes them by the same rules. What differs is how a value is classified: by value or by reference, in registers or through a hidden pointer, and whether an aggregate is split into its fields.

## Supported processors

| Processor module | Targets                                                   |
| ---------------- | --------------------------------------------------------- |
| x86/x64          | x86 (32-bit), x86-64 System V (Linux, macOS), Windows x64 |
| ARM              | AArch64; 32-bit ARM, soft-float and hard-float (VFP)      |
| RISC-V           | RV32 and RV64, with or without floating-point registers   |
| MIPS             | o32 and n64                                               |
| PowerPC          | 32-bit and 64-bit                                         |

The other processor modules and the other MIPS ABIs have no Rust-specific rules.

## Where `__rust` comes from

IDA applies `__rust` from the following sources:

* the DWARF plugin
* the PDB plugin
* the [Rust plugin](https://docs.hex-rays.com/add-ons/plugins/concepts/plugins-shipped-with-ida/rust-plugin.md), as the default calling convention of a Rust binary
* the decompiler
* a declaration you enter yourself, for example `u64 __rust add(u64 a, u64 b);`

## Basic notions

* **Slot (*****N*****)**: the width of a general-purpose register: 8 bytes on 64-bit targets, 4 bytes on 32-bit targets.
* **Leaves**: the scalar fields of a type (integers, pointers, floats) after nested structures are flattened.
* **One-field struct**: a struct with a single non-zero-sized field (tuple or named), or a closure that captures one variable. It behaves exactly like its field, unless it is `#[repr(C)]`: then it is an ordinary aggregate.
* **Enum**: a union of its variants. The leaves of all variants are merged: a leaf at the same offset, with the same size and kind, counts once.
* **ScalarPair**: a struct or enum with exactly two leaves, no arrays or bitfields, and its natural alignment (neither packed nor over-aligned).

## Differences from the C ABI

### Shape decides, not size

In C, an aggregate that fits in two registers usually travels in registers. In Rust, only scalars and ScalarPairs do; any other aggregate larger than one slot goes through memory.

### Small aggregates use an integer register

Any other aggregate of at most one slot (a small array, a packed struct, three small fields, a `#[repr(C)]` struct) is passed and returned in one integer register, even if it holds only floats: `struct { v: [f32; 2] }` comes back in `rax`/`X0`, where C uses floating-point registers.

### Return value

| Return type                         | Rust                             |
| ----------------------------------- | -------------------------------- |
| zero-sized                          | nothing                          |
| any type of at most *N* bytes       | registers                        |
| a scalar of at most 2·*N* bytes     | registers                        |
| a ScalarPair of at most 2·*N* bytes | registers, one leaf per register |
| anything else                       | hidden pointer                   |

For example, `struct { a: u32, b: u32, c: u32 }` or `[u8; 16]` come back through the hidden pointer, where C uses two registers.

### Arguments

An argument is passed by value if it is at most *N* bytes, a scalar, or a ScalarPair of any size. Anything else is passed by reference: its type becomes a pointer that takes one slot. C instead copies aggregates by value up to a size limit.

### ScalarPairs are split

Each leaf is passed as a separate argument of its own type: integer leaves in integer registers, float leaves in floating-point registers, one stack slot per leaf. This happens even when C would pack both leaves into one register: `struct { a: u8, b: u8 }` takes two registers in Rust and one in C. A returned pair also comes back one leaf per register.

### Zero-sized arguments

Zero-sized arguments (`()`, empty structs) take neither a register nor stack space.

### What stays as in C

The argument and return registers, their distribution among the arguments (integer/floating-point sharing, alignment of register pairs, splitting between registers and stack, stack layout), and where the hidden return pointer is passed.

## Limitations

* **Optimized builds**: rustc may give internal functions a private convention that does not follow `__rust`.
* **Float-only small aggregates**: IDA currently passes and returns small aggregates that consist only of floats (`[f32; 2]`) in a floating-point register on most targets that have them, instead of an integer register.
* **One-field structs**: the debug information does not show whether a struct with one named field is `#[repr(C)]`. IDA handles it like its field, with a few target-specific exceptions that follow the `#[repr(C)]` rule.
* **128-bit integers**: `u128` and `i128` are wider than two registers on 32-bit targets, so they are always returned through the hidden pointer. As arguments, they are treated differently depending on the target: most pass them by value in registers and on the stack, while some (RV32) pass them by reference, as their C convention does.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.hex-rays.com/core/types/concepts/rust-calling-convention.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
