liveBook · Manning

Calling convention

As explained in Refactoring to Rust

A C calling convention specifies how function arguments, return values, and calls are represented so compiled functions can interoperate with C.

A C calling convention defines how function arguments, return values, and calls are represented so compiled Rust and C functions can interoperate. Marking a Rust function with extern "C" selects this convention; exporting it also requires a C-compatible dynamic library and a stable symbol name. section 3.2.2

Exporting a Rust function for C

A calculator program declares solve and links against a Rust dynamic library. The Rust definition uses #[no_mangle] and extern "C", with raw C-compatible pointers and an integer return value. After Cargo builds the library, gcc can link the C program and call solve.

How Refactoring to Rust covers it

How deep it goes
Explains the convention through a working calculator integration, stopping short of broader ABI details.
Examples
Uses a C calculator calling Rust solve through a linked dynamic library.
What it assumes
Assumes basic C compilation and linking, while demonstrating the required Cargo library configuration and Rust export syntax.
What you can do after
Apply it via Rust's extern "C" function declarations when exposing Rust functions to C programs.
How it connects
Ties calling conventions to C FFI, dynamic libraries, symbol export, Cargo, linkers, and unsafe Rust.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Refactoring to Rust. The section links point to the parts of the book it draws on.