On difference between process::exit_code and ExitCode
โ Rust ๐ 2025-12-04 ๐ค surdeus ๐๏ธ 2Hi all,
I'm maintaining Hurl, a HTTP cli.
In our CI, we run Valgrind on every commit. In a recent commit, a code modification has triggered a memory leak. We isolate the code that triggers it and it boils down to:
use std::{env, process};
use std::collections::HashMap;
fn main() {
let _map: HashMap<String, String> = env::vars().collect();
process::exit(0);
}
With the help of the cargo-valgrind maintainer, we fix it by changing the main function signature from fn main() to fn main() -> ExitCode and avoiding process::exit function. Indeed, in the Rust std documentation, it is stated that
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; or, preferably, simply return a type implementing Termination (
such as ExitCode or Result) from the main function and avoid this function
What I would like to understand is where could I see the "magic" explaining why/how a main's returning an ExitCode fixes this leak. Basically, what's the difference between fn main() and fn main() -> ExitCode that explains that all resources all well dropped.
Thanks a lot,
Jc
3 posts - 2 participants
๐ท๏ธ Rust_feed