Search code examples
rustffi

What's the difference between extern fn and extern "C" fn in Rust?


I've tried reading various github issues trying to track down what the difference is and just ended up confused.

#[no_mangle]
pub extern fn foo() {
   ...
}

vs.

#[no_mangle]
pub extern "C" fn foo() {
   ...
}

Solution

  • There is no difference because, as the reference says:

    By default external blocks assume that the library they are calling uses the standard C ABI on the specific platform.

    extern "C" -- This is the same as extern fn foo(); whatever the default your C compiler supports.

    An issue was created to always require explicitly stating extern "C" but the RFC has been refused.

    There is an issue in fmt-rfcs about "should we format extern "C" fn as that or extern fn?".