rust-impl-ffi-bindgen
rust-impl-ffi-bindgen
C interop has two directions, and each has a generator. To call a C library FROM Rust, write unsafe extern "C" { ... } declarations (by hand for small APIs) or run bindgen against the header (for everything else). To expose a Rust library TO C, mark the exported fn items with extern "C" plus #[unsafe(no_mangle)] and run cbindgen to emit the matching .h file. The ABI surface in between is governed by #[repr(C)] types, opaque pointers, null-terminated strings (CString / CStr), function pointers, and Drop guards for foreign-allocated resources. On Unix, file descriptors crossing the boundary use OwnedFd (owning, closes on Drop) and BorrowedFd<'_> (non-owning, lifetime-bounded), never raw RawFd for ownership-bearing APIs.
Cross-references: [[rust-syntax-unsafe]] (unsafe operations, UB categories, unsafe extern rationale), [[rust-syntax-edition-2024]] (mandatory unsafe extern + #[unsafe(no_mangle)]), [[rust-impl-build-scripts]] (build.rs to invoke bindgen / cbindgen, cargo:rustc-link-lib), [[rust-impl-cargo-project]] (build-dependencies, links key).