std-method
Return a CString to its original Rust allocator
into_raw relinquishes Rust ownership. Reclaim the exact pointer once with from_raw, only if it originated from into_raw and its string length has not changed. Do not use C free on this pointer. Real FFI APIs should provide an explicit matching release function. Expected stdout (not executed): owned Common mistake: Calling from_raw on a C malloc pointer or reclaiming the same pointer twice.
Formal Syntax & Structure
rust syntaxrust
let ptr = value.into_raw(); unsafe { CString::from_raw(ptr) }Working Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { use std::ffi::CString; let ptr = CString::new("owned").unwrap().into_raw(); // SAFETY: this is the original pointer; no mutation or intervening free occurred. let reclaimed = unsafe { CString::from_raw(ptr) }; println!("{}", reclaimed.to_str().unwrap());}