55

Is there a way of returning an exit code in Rust 1.0?

I've tried env::set_exit_status(exit_code); but this generates a compiler error.

There is also this question: Exit Rust program early which is similar but asks about the case when the process has to be exited early.


EDIT: I'm looking for a solution that will also allow the process to tidy up the stack, call destructors, etc.

Community
  • 1
  • 1
Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139
  • 2
    @static_rtti Can you explain further what part of [Levans' answer](http://stackoverflow.com/a/30285110/155423) is unsuitable? *Surely a systems programming language ...* remember, that Rust is also to be used in environments where there is no launcher process (like a kernel). – Shepmaster Jun 17 '15 at 19:01
  • I hadn't read it well enough, it sounds like what I want. – static_rtti Jun 18 '15 at 07:57

3 Answers3

58

Building over the comments of @FrancisGagné 's answer, if you are searching for an equivalent of C's return exit_code, you can artificially build it this way:

fn main() {
    let exit_code = real_main();
    std::process::exit(exit_code);
}

fn real_main() -> i32 {
    // the real program here
}

This way, all the objects of your program will be in the scope of the real_main() function, and you can safely use return exit_code; in main while still having all destructors properly run.

Thomas
  • 174,939
  • 50
  • 355
  • 478
Levans
  • 14,196
  • 3
  • 49
  • 53
33

Starting with Rust 1.26, main can return any type that implements the Termination trait. The standard library provides implementations on several types, such as Result<(), E> for any type E: Debug. Furthermore, the trait was stabilized in 1.61, allowing third-party crates to implement it for their own types.

For Result values, an Ok value maps to ExitCode::SUCCESS (usually 0) and an Err value maps to ExitCode::FAILURE (usually 1). The error value is also automatically printed to the standard error stream.

If you need to return a specific numeric value, use ExitCode as the return type for main.

use std::process::ExitCode;

fn main() -> ExitCode {
    ExitCode::from(101)
}
Francis Gagné
  • 60,274
  • 7
  • 180
  • 155
  • Hmm, Rust 1.60 shows me: "`main` function is not allowed to have generic parameters `main` cannot have generic parameters [rustc(E0131)]" – MBODM Jul 10 '22 at 10:03
  • Since my StackOverflow edit time is already over, i had to add a new comment here. This is the solution for my above statement: https://stackoverflow.com/a/50459909/1750923 – MBODM Jul 10 '22 at 10:10
  • As of Rust 1.61.0, the `Termination` trait is now stable! Hurray! https://blog.rust-lang.org/2022/05/19/Rust-1.61.0.html – Senkwich Jul 25 '22 at 20:03
13

std::process::exit exits the program with the specified exit code.

Francis Gagné
  • 60,274
  • 7
  • 180
  • 155
  • 11
    Unfortunately it doesn't clean up and call destructors. I'll amend the question to mention that. – Thomas Bratt May 16 '15 at 22:09
  • 1
    Any reason why you need the clean up to happen? Do you have destructors that do "interesting" stuff? If not, then letting the operating system clean up the process is often good enough. – Francis Gagné May 16 '15 at 22:18
  • 2
    I guess I'm looking for a direct replacement for `return exit_code;` in C. I'd like to avoid the possibility of not flushing to disk etc if at all possible. – Thomas Bratt May 16 '15 at 22:22
  • @ThomasBratt That's exactly what you get. A return in C doesn't call any desctructors either as (standard) C doesn't have destructors. – Pavel Šimerda Sep 24 '22 at 21:35