Interop Basics
10 examples for choosing how Rust talks to Python, Node.js, C/C++, and other runtimes.
Busca en todas las páginas de la documentación
10 examples for choosing how Rust talks to Python, Node.js, C/C++, and other runtimes.
| Goal | Primary tool |
|---|---|
| Python extension module | PyO3 + maturin |
| Node native addon | napi-rs |
| C/C++ static or dynamic lib | extern "C" + cbindgen |
| Browser compute | wasm-bindgen |
| Sandboxed plugin | WASM component / WASI |
Pick one primary boundary per integration point.
main?Rust owns main -> embed Python/JS via FFI (PyO3, Neon)
Host owns main -> export Rust as library (cdylib/staticlib)The owner controls initialization, signal handling, and allocator choice.
#[repr(C)]
pub struct Point {
pub x: f64,
pub y: f64,
}repr(C) and extern "C"String or Vec in C headersRelated: Exposing a Rust Library to C/C++
#[repr(C)]
pub enum Status {
Ok = 0,
InvalidInput = 1,
Oom = 2,
}Map Rust Result to integer codes or out-parameters at the boundary.
Rust allocates -> Rust frees (export mylib_free)
C allocates -> C frees
Copy at boundary -> safest default for small dataDocument who frees every pointer in the header.
Related: Data Marshalling
use pyo3::prelude::*;
#[pyfunction]
fn add(a: i64, b: i64) -> PyResult<i64> {
Ok(a + b)
}Python calls Rust for hot paths. Rust calls Python for orchestration when needed.
Related: PyO3
#[napi]
pub fn fib(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
_ => fib(n - 1) + fib(n - 2),
}
}Generates TypeScript types for Node consumers automatically.
Related: Node.js with napi-rs
Related: Embedding Rust in Other Runtimes
maturin develop
python -c "import myrust; print(myrust.add(1, 2))"Builds wheels with Rust inside for pip install.
Related: maturin
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 16 jul 2026