expression
Declare a Rust–C++ boundary with CXX
CXX generates glue on both language sides from a restricted bridge declaration. Here the C++ implementation is noexcept and uses fixed-width scalar values. The unsafe extern block asserts the declared safe-call contract has been reviewed; it does not prove arbitrary native implementations correct. CXX supports selected shared and opaque types rather than arbitrary Rust or C++ layouts. Expected lab stdout (not executed): 42 Common mistake: Treating a checked signature as proof that the C++ body is thread-safe or memory-safe. This is part of a multi-file lab, not a standalone playground snippet.
Formal Syntax & Structure
rust syntaxrust
#[cxx::bridge] mod ffi { unsafe extern "C++" { ... } }Working Example
Example Snippetrust
#[cxx::bridge(namespace = "bm106")]mod ffi { // SAFETY: the matching native definition accepts every u16 and cannot throw. unsafe extern "C++" { include!("bm106-cxx/include/scale.h"); fn scale(input: u16) -> u32; }}fn main() { println!("{}", ffi::scale(21)); }