Skip to content

Commit b0d18be

Browse files
committed
Return ExitCode from main()
It's not great to invoke std::process::exit() directly, because a program exited this way is not cleaned up properly: destructors may not run and stdio buffers not flushed. This is a potential cause of bugs and in fact it can be avoided by returning an ExitCode from main() instead. So do just that.
1 parent bab9afa commit b0d18be

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/main.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
// Copyright (C) 2020-2023 The cargo-http-registry Developers
1+
// Copyright (C) 2020-2025 The cargo-http-registry Developers
22
// SPDX-License-Identifier: GPL-3.0-or-later
33

4-
use std::io::stdout;
5-
use std::io::Write as _;
64
use std::net::SocketAddr;
75
use std::path::PathBuf;
8-
use std::process::exit;
6+
use std::process::ExitCode;
97

108
use anyhow::Context as _;
119
use anyhow::Result;
@@ -61,14 +59,9 @@ fn run() -> Result<()> {
6159
Ok(())
6260
}
6361

64-
fn main() {
65-
let exit_code = run()
66-
.map(|_| 0)
62+
fn main() -> ExitCode {
63+
run()
64+
.map(|_| ExitCode::SUCCESS)
6765
.map_err(|e| eprintln!("{e:?}"))
68-
.unwrap_or(1);
69-
70-
// We exit the process the hard way next, so make sure to flush
71-
// buffered content.
72-
let _ = stdout().flush();
73-
exit(exit_code)
66+
.unwrap_or(ExitCode::FAILURE)
7467
}

0 commit comments

Comments
 (0)