← ClaudeAtlas

rust-ffilisted

Call C or C++ from Rust (or vice versa) correctly — string/representation mismatches, error-handling conventions, ownership across the FFI boundary, and choosing between bindgen (C) and cxx (C++). Use when writing an extern "C" binding, wrapping a C/C++ library, converting between C strings and Rust &str, or reviewing an FFI boundary for soundness.
takurot/rust-skills-comprehensive · ★ 0 · AI & Automation · score 71
Install: claude install-skill takurot/rust-skills-comprehensive
# Rust FFI For the mechanics of `unsafe extern "C"` declarations and calling unsafe functions in general, see `rust-unsafe-fundamentals`. This skill is about the FFI boundary itself: what differs between Rust and C/C++, and how to bridge it soundly rather than just make it compile. ## Interop strategy: go through the C ABI, not directly Rust and C++ (or any two languages) generally **cannot** share data structures and call each other's functions directly — their type layouts, calling conventions, and runtime models don't agree. The practical path both directions go through is the **C ABI** as a lowest common denominator: ``` Rust <-----> C ABI <-----> C++ ``` This is why C interop (raw `extern "C"`, or the `bindgen` tool) is comparatively straightforward, while C++ interop needs a bridging layer (the `cxx` crate, or hand-written `extern "C"` shims) to translate C++'s richer type system down to something C-ABI-compatible and back up on the other side. (Fully automatic high-fidelity interop across the *entire* type system — e.g. Crubit, Zngur — exists but is experimental; don't expect it to eliminate this boundary today.) ## Rust ↔ C: what actually differs | Concern | Rust | C | |---|---|---| | Errors | `Result<T, E>`, `Option<T>` | Sentinel return values, out-parameters, global `errno` | | Strings | `&str`/`String` — UTF-8, length carried alongside the pointer | `char*` — NUL-terminated, encoding unspecified | | Nullability | Explicit: absence is `Option<T>`, a plai