Low-level interaction with the operating system

RecipeCratesCategories
Call libc, the C standard librarylibccat-os

Call libc, the C standard library

libc libc-crates.io libc-github libc-lib.rs cat-external-ffi-bindings cat-os cat-no-std

Bindings for directly calling libc functions.

use std::ffi::CString;

use libc::c_char;

// Declare an external C function puts that takes a pointer to a c_char (C
// string) and returns an i32.
unsafe extern "C" {
    fn puts(s: *const c_char) -> i32;
}

fn main() {
    // create a CString containing the message we want to print and call the
    // puts function inside an unsafe block, since interacting with C code is
    // considered unsafe in Rust.
    let c_string = CString::new("Hello from C!").expect("CString::new failed");
    unsafe {
        puts(c_string.as_ptr());
    }
}