Calling External Code

Functions from other languages might violate the guarantees of Rust. Calling them is thus unsafe:

  1. extern "C" {
  2. fn abs(input: i32) -> i32;
  3. }
  4. fn main() {
  5. unsafe {
  6. // Undefined behavior if abs misbehaves.
  7. println!("Absolute value of -3 according to C: {}", abs(-3));
  8. }
  9. }

This is usually only a problem for extern functions which do things with pointers which might violate Rust’s memory model, but in general any C function might have undefined behaviour under any arbitrary circumstances.

The "C" in this example is the ABI; other ABIs are available too.