Start with the idea
extern "C" fixes the calling convention, repr(C) fixes the layout, and everything about ownership becomes a convention you have to document. Who frees this pointer, may it be null, how long is it valid, and is it aliased are the four questions each call has to answer — and the safe wrapper you write around it is where those answers are enforced.
An FFI call joins two type systems at an ABI boundary. extern "C" selects the C calling convention, and #[repr(C)] requests a compatible struct layout for the target, including padding. The declarations on both sides must agree on integer widths, pointer types, alignment, and ownership. The Rust compiler cannot inspect a C header during an ordinary extern call to prove the declaration matches. A successful trivial call does not validate every edge case.
Take a function accepting a pointer and length.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.