extern - Rust
Description: API documentation for the Rust `extern` keyword in crate `std`.
Keywords: rust, rustlang, rust-lang, extern

# [−]Keyword extern

For external connections in Rust code.

The `extern` keyword is used in two places in Rust. One is in conjunction with the `crate` keyword to make your Rust code aware of other Rust crates in your project, i.e. `extern crate lazy_static;`. The other use is in foreign function interfaces (FFI).

`extern` is used in two different contexts within FFI. The first is in the form of external blocks, for declaring function interfaces that Rust code can call foreign code by.

ⓘThis example is not tested

    #[link(name = "my_c_library")]
    extern "C" {
        fn my_c_function(x: i32) -> bool;
    }Run

This code would attempt to link with `libmy_c_library.so` on unix-like systems and `my_c_library.dll` on Windows at runtime, and panic if it can't find something to link to. Rust code could then use `my_c_function` as if it were any other unsafe Rust function. Working with non-Rust languages and FFI is inherently unsafe, so wrappers are usually built around C APIs.

The mirror use case of FFI is also done via the `extern` keyword:

    #[no_mangle]
    pub extern fn callable_from_c(x: i32) -> bool {
        x % 3 == 0
    }Run

If compiled as a dylib, the resulting .so could then be linked to from a C library, and the function could be used as if it was from any other library.

For more information on FFI, check the Rust book or the Reference.

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)