Introducing FFIs
In this section, we'll understand what FFI is, and then see the two unsafe Rust features related to FFI.
To understand FFI, let's look at the following two examples:
- There is a blazing-fast machine learning algorithm written in Rust for linear regression. A Java or Python developer wants to use this Rust library. How can this be done?
- You want to make Linux syscalls without using the Rust Standard Library (which essentially means you want to either implement a feature that's not available in the standard library or want to improve an existing feature). How would you do it?
While there may be other ways to solve this problem, one popular method is to use FFI.
In the first example, you can wrap the Rust library with an FFI defined in Java or Python. In the second example, Rust has a keyword, extern
, with which an FFI to a C function can be set up and called. Let's see an example of the second case next:
use std::ffi::...