C++ Academy · Lesson

C++ for Rust FFI Boundaries

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

Lesson 3 of 414 steps

C++ for Rust FFI Boundaries is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

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

Yes — any 3 lessons of the C++ Academy learning path, including “C++ for Rust FFI Boundaries”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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