Calling C Code From Rust

I recently came across the extern keyword in the official Rust book. In a nutshell, one of the things it allows you to do is use a Foreign Function Interface (FFI) to call code implemented in a language like C.

The book provides a great example of calling the abs function from the C standard library. After going through this example, however, I was curious as to how I can write my own trivial C library and get my Rust code to call it. I ended up implementing a function in C called calculate_add_three which does exactly what you would think: it takes an integer, adds three, and returns the result. After doing some research, I was able to set up a build script in my Rust directory (you can read more about Build Scripts here) which uses the cc crate to link my C code to my Rust code.

The full repository with my work is here. The most interesting parts are the build script which I mentioned earlier, the script that compiles my C library code, and the main.rs file which uses the extern keyword to call my local C code.