expression
Separate ABI choice from unsafe obligations
A calling convention alone does not establish pointer validity. Foreign declarations in edition 2024 require unsafe extern blocks; the declaration author must verify signatures. This fixture uses a Rust-defined function with the C ABI, not a linked foreign library. Real FFI also needs ABI, ownership and unwind review. Expected stdout (not executed): 12 Common mistake: Assuming extern "C" automatically makes a signature or call safe.
Formal Syntax & Structure
rust syntaxrust
unsafe extern "C" fn read(p: *const i32) -> i32Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { /// # Safety /// p must be aligned and valid to read an initialized i32; /// no conflicting access may occur during the call. unsafe extern "C" fn read(p: *const i32) -> i32 { // SAFETY: caller must satisfy the read contract. unsafe { *p } } let value = 12; let callback: unsafe extern "C" fn(*const i32) -> i32 = read; // SAFETY: value is alive and shared for the duration of the call. println!("{}", unsafe { callback(&value) });}