How to Use the libc Crate in Rust

Add libc to Cargo.toml and import functions with use statements to call C library APIs from Rust.

You use the libc crate by adding it as a dependency in Cargo.toml and importing its types or functions with a use statement in your source code.

[dependencies]
libc = "0.2"
use std::ffi::CString;

fn main() {
    let msg = CString::new("Hello from libc!\n").unwrap();
    unsafe {
        libc::printf(msg.as_ptr());
    }
}