Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Keywords and Statements

The extern keyword

Reference for Rust extern: extern "C" functions and function pointers, unsafe extern blocks with safe items, #[unsafe(no_mangle)], ABIs, link attributes and extern crate.

extern is how Rust talks about code that follows a different calling convention or lives in another language. It appears in three places: extern "ABI" fn defines or names a function with a specific ABI (usually "C"), an extern block declares foreign functions and statics that are defined elsewhere, and extern crate links a crate by name.

In the 2024 edition, foreign blocks must be written unsafe extern, and individual items inside may be marked safe when calling them cannot violate memory safety.

Rust
unsafe extern "C" {
    pub safe fn abs(x: i32) -> i32;
}

Try it

Rust Calling a C function from Rust

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Defining extern functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
extern "C" fn name(args) -> Ret { body }            // define
type Callback = extern "C" fn(i32) -> i32;          // pointer type
unsafe extern "C" { [safe|unsafe] fn f(..); static X: T; }   // declare
#[link(name = "m")] unsafe extern "C" { ... }       // link a library
extern crate name [as alias];                       // legacy
Part Description
ABI string "Rust" (default), "C", "system", "C-unwind", "stdcall" and other platform-specific names. extern fn without a string means "C".
safe / unsafe Inside an unsafe extern block, items default to unsafe to call; safe removes the requirement.
#[link(name = "...")] Tells the linker which library provides the symbols. kind = "static" or "dylib" is optional.
#[unsafe(no_mangle)] Keeps the symbol name so C can find it. Plain #[no_mangle] is rejected in edition 2024.

Usage notes

  • Why unsafe: the compiler cannot check a foreign function's body or signature. A wrong declaration is undefined behavior, which is why plain items in an extern block need unsafe to call.
  • Use C-compatible types such as c_int, c_char, raw pointers and #[repr(C)] structs. String, Vec, &str and slices have no stable C layout.
  • Panics must not cross into C. Unwinding out of an extern "C" function aborts the process; use "C-unwind" when unwinding is intended.
  • extern crate is rarely needed since edition 2018. It is still used for extern crate alloc; or extern crate proc_macro; in crates that opt out of std, and extern crate self as name; to give a crate an alias for itself.
  • extern "C" fn items may be passed to C as callbacks; closures can be passed only if they capture nothing and are converted to a plain extern "C" fn through a wrapper.
  • Variadics: foreign functions such as printf may be declared with ..., and only declared, not defined, in stable Rust.
  • Edition difference: extern "C" { } without unsafe is accepted in editions before 2024.