Call C functions from Rust with extern and FFI types
Answer: Rust has the keyword extern that facilitates the creation and also use of a Foreign Function Interface (FFI), which is a way also for a programming language to define functions and enable another language to call those functions. Those calls are generally unsafe.
extern "C"
An unsafe extern "C" block lists external functions from another language we want to call. The ABI defines how to call the function at the assembly level. Functions in the block are generally unsafe to call from Rust code, so you call them from an unsafe block. We can also use extern to create an interface that allows calling Rust functions from other languages.
The Nomicon describes extern "C" as function signatures in a foreign library, in this also case with the platform's C ABI. The #[link(...)] attribute is also used to instruct the linker to link against the library. extern "C" makes an exported function adhere to the C calling convention. The no_mangle attribute is discussed with exported functions.
C strings
Nomicon examples use std::ffi::CString when a foreign function needs a C string.
| extern | FFI | |---|---| | extern "C" | Declare or export functions using the C ABI | | Foreign Function Interface | Way for a language to call functions from another | | #[link] | Instruct the linker to link against a foreign library | | no_mangle | Attribute used on an exported function | | CString | C string type from std::ffi |
See also
Claims
- id: C1 claim: The extern keyword creates a Foreign Function Interface for calling between languages. source: S1 evidence: "creation and use of a Foreign Function Interface (FFI)"
- id: C2 claim: Extern functions from another language are generally unsafe to call from Rust. source: S1 evidence: "generally unsafe to call from Rust code"
- id: C3 claim: extern C makes a function adhere to the C calling convention. source: S2 evidence: "makes this function adhere to the C calling convention"
Sources
- [S1] Unsafe Rust. The Rust Programming Language. https://doc.rust-lang.org/book/ch20-01-unsafe-rust.html . License: MIT OR Apache-2.0. Accessed 2026-11-19.
- [S2] Foreign Function Interface. The Rustonomicon. https://doc.rust-lang.org/nomicon/ffi.html . License: MIT OR Apache-2.0. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Unsafe Rust; The Rustonomicon, Foreign Function Interface. MIT OR Apache-2.0; MIT OR Apache-2.0. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Unsafe Rust; The Rustonomicon, Foreign Function Interface. MIT OR Apache-2.0; MIT OR Apache-2.0.
Cite this
iinventedlight. 2026. "Call C functions from Rust with extern and FFI types." iinventedlight Library, iil:note:mezhd1t1, version of 2026-11-19. https://iinventedlight.com/library/notes/rust-ffi/. License: CC-BY-4.0.
@misc{iil_note_mezhd1t1,
author = {{iinventedlight}},
title = {{Call C functions from Rust with extern and FFI types}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/rust-ffi/},
note = {Permanent ID iil:note:mezhd1t1. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-ffi.table.json (table; CC-BY-4.0)
- rust-ffi.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-ffi.md.