Unsafe External Functions
Functions in a foreign language may also be unsafe:
Speaker Notes
- Rust used to consider all extern functions unsafe, but this changed in Rust
1.82 with
unsafe extern
blocks. abs
must be explicitly marked assafe
because it is an external function (FFI). Calling external functions is usually only a problem when those functions do things with pointers which 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. - Note that there is no verification that the Rust function signature matches that of the function definition – that’s up to you!