Calling Rust from other languages
Rust code can be called from any language that can call C. The Rust library should have the crate-type
value "dylib"
. When rustfn1
is the Rust function to be called, this must be declared as:
#[no_mangle]pub extern "C" fn rustfn1() { }
#[no_mangle]
serves to keep the function names plain and simple, so that they are easier to link to. The "C"
exports the function to the outside world with the C calling convention.
Some examples to call Rust from C itself, Python, Haskell, and Node.js can be found in this article https://zsiciarz.github.io/24daysofrust/book/vol1/day23.html. How to call Rust from Perl and Julia is detailed at http://paul.woolcock.us/posts/rust-perl-julia-ffi.html.
Note
For a nice collection of examples, see http://jakegoulding.com/rust-ffi-omnibus/.