expression

Translate Rust errors to stable C status codes

Rust Result is not a general C ABI return type. Define numeric status codes and their meaning in the interface contract; do not expose Rust implementation details. This example uses a pure validation function and no output pointers. Expected stdout (not executed): 0 1 Common mistake: Returning a Rust Result or String directly in a C ABI signature.

Formal Syntax & Structure

rust syntaxrust
extern "C" fn(...) -> c_int

Working Example

Example Snippetrust
#![deny(unsafe_op_in_unsafe_fn)]fn main() {    use std::ffi::c_int;    fn validate(n: c_int) -> Result<(), ()> { if n >= 0 { Ok(()) } else { Err(()) } }    extern "C" fn status(n: c_int) -> c_int {        match validate(n) { Ok(()) => 0, Err(()) => 1 }    }    println!("{} {}", status(4), status(-4));}
Rust Reference