expression
Status codes and output parameters
A foreign function can return a status and write an output only on success. A safe wrapper must check the status before consuming the output. This local ABI function accepts a valid writable pointer or null and does not retain it. Expected stdout (not executed): Ok(17) Common mistake: Assuming an output parameter is valid after an error status.
Formal Syntax & Structure
rust syntaxrust
unsafe extern "C" fn(*mut u32) -> c_intWorking Example
Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() { use std::ffi::c_int; unsafe extern "C" fn read_value(out: *mut u32) -> c_int { if out.is_null() { return 1; } // SAFETY: contract requires a writable aligned u32 on non-null input. unsafe { out.write(17); } 0 } fn read() -> Result<u32, c_int> { let mut out = 0_u32; // SAFETY: out is a local initialized writable u32. let code = unsafe { read_value(&mut out) }; if code == 0 { Ok(out) } else { Err(code) } } println!("{:?}", read());}