RRust By Example
advancedUnsafe

FFI with C

Call C functions from Rust using FFI.

FFI with C

Call C functions from Rust using FFI.

Difficulty

Advanced

Code

rust
extern "C" {
    fn abs(input: i32) -> i32;
}

fn main() {
    unsafe {
        println!("abs(-3) = {}", abs(-3));
        println!("abs(42) = {}", abs(42));
    }
}

Explanation

FFI calls functions from other languages.

Key Concepts

  • Read the code carefully and understand the data flow
  • Try modifying the example to see how it changes behavior
  • Run this code in the Rust Playground

Related Topics

Browse more examples in the unsafe category to build a complete understanding of this topic.

More Unsafe Examples