0Pricing
C++ Academy · Lesson

C++ for Rust FFI Boundaries

Expose C++ functions for Rust FFI consumption and the reverse direction.

C++ for Rust FFI Boundaries is a free C++ Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

C++ and Rust

Rust and C++ are both compiled, low-level languages — but they have different ABIs and incompatible type systems. They typically interop via a C ABI bridge.

Rust to C++: extern "C"

Export Rust functions with extern "C" and #[no_mangle].

// Rust
#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
    a + b
}

C++ Calling Rust

Declare the Rust function in C++ and link with the Rust static library.

// C++
extern "C" int add(int a, int b);

std::cout << add(2, 3);

C++ to Rust

Reverse direction — Rust calls C++. Wrap C++ in extern "C" and declare in Rust.

// C++
extern "C" int multiply(int a, int b) {
    return a * b;
}

// Rust
extern "C" {
    fn multiply(a: i32, b: i32) -> i32;
}

Linking Static Libraries

Build the Rust crate as a staticlib or cdylib and link with the C++ build.

# Cargo.toml
[lib]
crate-type = ["staticlib"]

Bindgen and Cbindgen

Auto-generate FFI declarations:

  • bindgen — generate Rust bindings from C++ headers
  • cbindgen — generate C/C++ headers from Rust code

cxx Crate

The cxx crate provides safer, higher-level C++/Rust interop. Declare a shared schema; cxx generates bindings on both sides with strong typing.

Memory Ownership Across FFI

Both languages have RAII but different ownership models. Pass owned data by value, borrowed data by raw pointer — and document who frees what.

Strings Across FFI

Rust String and C++ std::string are incompatible. Use *const c_char (null-terminated) or (*const u8, usize) (pointer + length).

Exceptions vs panic

Rust panics and C++ exceptions do not cross FFI safely. Catch C++ exceptions at the boundary; use std::panic::catch_unwind on the Rust side.

Build Integration

Use a meta-build system: CMake calling Cargo, or Cargo calling CMake via a build script (build.rs). The cxx crate handles common patterns.

Real-World Examples

Many production codebases mix C++ and Rust: Mozilla Firefox uses Rust components inside a C++ engine; Chromium experiments with Rust modules; many crypto libraries provide both bindings.

Quick Check

Which Rust attribute disables name mangling so C/C++ can find the symbol?

Recap

C++ and Rust interop via the C ABI. Use extern "C" and #[no_mangle]. Generate bindings with bindgen and cbindgen, or use the cxx crate for safer typed bridges. Handle ownership, strings, and panics carefully across the boundary.

Frequently asked questions

Is the “C++ for Rust FFI Boundaries” lesson free?

Yes — the full text of “C++ for Rust FFI Boundaries” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “C++ for Rust FFI Boundaries”?

Expose C++ functions for Rust FFI consumption and the reverse direction. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “C++ for Rust FFI Boundaries” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Extern C and the C ABI
  2. Calling Python from C++ via pybind11
  3. C++ for Rust FFI Boundaries
  4. Using SWIG for Multi-Language Bindings
← Back to C++ Academy