Interfacing with C
Because of the vast functionality that exists in C code, it can sometimes be useful to delegate processing to a C routine, instead of writing everything in Rust.
You can call all functions and types from the C standard library by using the libc
crate, which must be obtained through Cargo.
You have to add the following to Cargo.toml:
[dependencies] libc = "*"
If you use Rust's nightly version, this is not necessary, but you have to use a feature attribute (or a feature gate, as they are also called) at the start of your code:
#![feature(libc)]
Note
Feature gates are common in Rust to enable the use of a certain functionality, but they are not available in stable Rust, only in the current development branch (nightly release).
If you have both the stable and nightly versions installed, you can easily switch between them using the commands rustup default nightly
and rustup default stable
.
Then, simply add the following to your Rust code:
extern crate libc;
To import C functions...