std-module

std::ffi — interoperable strings and C types

This module contains types for foreign-language boundaries. A C string is a sequence of nonzero bytes ending with a zero byte, not necessarily UTF-8. CString owns such a sequence; CStr borrows it. C aliases match the target C ABI rather than promising a universal width. Expected stdout (not executed): hello 0 Common mistake: Passing a Rust String directly as a C string or assuming C int is always the same size.

Formal Syntax & Structure

rust syntaxrust
use std::ffi::{CStr, CString, c_int};

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::ffi::{CStr, CString, c_int};    let owned = CString::new("hello").unwrap();    let borrowed: &CStr = owned.as_c_str();    let status: c_int = 0;    println!("{} {status}", borrowed.to_str().unwrap());}
Rust Reference