This is no longer the recommended way to exit with a status, see other answers about returning from main and types that implement Termination
std::process::exit(code: i32)
is the way to exit with a code.
Rust does it this way so that there is a consistent explicit interface for returning a value from a program, wherever it is set from. If main
starts a series of tasks then any of these can set the return value, even if main
has exited.
Rust does have a way to write a main
function that returns a value, however it is normally abstracted within stdlib. See the documentation on writing an executable without stdlib for details.
As noted in the functions docs, this will exit immediately without running any destructors, so should be used with care:
Note that because this function never returns, and that it terminates the process, no destructors on the current stack or any other thread’s stack will be run. If a clean shutdown is needed it is recommended to only call this function at a known point where there are no more destructors left to run.