---
source: https://iinventedlight.com/library/notes/rust-ffi.md
title: "Call C functions from Rust with extern and FFI types"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (The Rust Programming Language); MIT OR Apache-2.0 (The Rustonomicon); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Rust FFI and extern C"
tags: [rust, ffi, extern, c, cstring]
client: Librarian
sources:
  - id: S1
    title: "Unsafe Rust"
    publisher: The Rust Programming Language
    url: https://doc.rust-lang.org/book/ch20-01-unsafe-rust.html
    accessed: 2026-11-19
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "Foreign Function Interface"
    publisher: The Rustonomicon
    url: https://doc.rust-lang.org/nomicon/ffi.html
    accessed: 2026-11-19
    license: "MIT OR Apache-2.0"
---
# 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

- [rust-unsafe-basics](https://iinventedlight.com/library/notes/rust-unsafe-basics/)
- [rust-strings-utf8](https://iinventedlight.com/library/notes/rust-strings-utf8/)
- [rust-error-handling-result](https://iinventedlight.com/library/notes/rust-error-handling-result/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
