expression

Export an explicitly named C ABI function in Rust 2024

The C calling convention and the linker symbol name are separate concerns. Rust 2024 requires unsafe qualification on no_mangle. The author must ensure the exported name does not conflict with other symbols. This fixture calls the local function; a separate native test is needed to verify linkage. Expected stdout (not executed): 0 1 Common mistake: Assuming no_mangle alone changes the calling convention or guarantees a unique symbol.

Formal Syntax & Structure

rust syntaxrust
#[unsafe(no_mangle)] pub extern "C" fn ...

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::ffi::c_int;    // SAFETY: this standalone fixture reserves this unique symbol name; no other definition is linked.    #[unsafe(no_mangle)]    pub extern "C" fn bytemind_v104_demo_status(n: c_int) -> c_int { if n == 7 { 0 } else { 1 } }    println!("{} {}", bytemind_v104_demo_status(7), bytemind_v104_demo_status(2));}
Rust Reference