keyword
Rust extern: specify a foreign calling convention
`extern` selects an ABI (calling convention) for a function or declares externally defined items in an extern block. In Rust 2024, extern blocks require `unsafe`. A Rust-defined C-ABI function does not automatically become an exported C symbol. Expected output (not executed): 12 Why: The function and pointer have matching C ABIs and signatures. Common mistake: Do not assume any Rust type has a compatible C representation.
Formal Syntax & Structure
rust syntaxrust
extern "C" fn name() {}Working Example
Example Snippetrust
extern "C" fn double(n: i32) -> i32 { n * 2 }
fn main() { let operation: extern "C" fn(i32) -> i32 = double; println!("{}", operation(6));}