expression
Return from main to allow local cleanup
Returning from main allows local values to be dropped as control leaves their scopes. Direct process::exit skips stack destructors. This example uses a guard to make local cleanup visible; it does not promise that other running threads finish automatically. Fixture: Expected stdout (not executed): work complete local cleanup Expected stderr: (empty) Expected exit: 0 Common mistake: Calling process::exit from deep inside a function that owns resources needing cleanup.
Formal Syntax & Structure
rust syntaxrust
fn main() -> std::process::ExitCodeWorking Example
Example Snippetrust
fn main() -> std::process::ExitCode { struct Guard; impl Drop for Guard { fn drop(&mut self) { println!("local cleanup"); } } let _guard = Guard; println!("work complete"); std::process::ExitCode::SUCCESS}