Low-level Interaction with the Operating System
Recipe | Crates | Categories |
---|---|---|
Call libc , the C Standard Library |
Call libc
, the C Standard Library
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()); } }