Call C Code from Rust

Hi, I seek up to date information regarding the standard means of calling C code from Rust. Any information and links to official documentation appreciated.

You're probably looking for the foreign function interface (FFI), see e.g. FFI - The Rustonomicon

To automate the process a bit more, have a look at Introduction - The bindgen User Guide

Ask back here if you still have more questions.

1 Like

Thanks much!

2 Likes

:jack_o_lantern:

Yes, the Rust compiler has no way to check whether the C code complies with Rust's safety guarantees. So that burden falls on you, with unsafe code.

Yes, I understood why it was named as such. :slight_smile: It's important to be able to use existing libraries that don't have a Rust counterpart yet, as well as be able to use existing code until it can be ported to Rust.

Thanks Again!

How about expose a rust function as a C function? Official documentation?

(Still early in familiarizing with Rust, but from looking at various rust samples/documentation, the Rust language appears to have a simple elegance. My background is C/C++/C# [and occasional past minor ventures in F#, Java, and Kotlin])