How to Call Rust Functions from C

Call Rust functions from C by using #[no_mangle] and pub extern "C" attributes, then compile as a static library and link with gcc.

You call Rust functions from C by marking the Rust function with #[no_mangle] and pub extern "C" to ensure C-compatible naming and calling conventions.

#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
    a + b
}

Compile the Rust code as a static library using rustc --crate-type=lib and link it with your C program using gcc.