Crate quit

source ·
Expand description

This crate allows cleanly exiting a program using a custom exit code, without the drawbacks of process::exit. Destructors will be called as usual, and the stack will be completely unwound.

It is always required to attach #[main] to the main function. Then, with_code can be called from almost anywhere in the program. Restrictions are noted in the documentation for that function.


Internally, this crate uses panicking to unwind the stack. Thus, if panicking were set to “abort” instead of the default “unwind”, setting the exit status would not work correctly. This crate will cause a compile error in that case, to avoid silent incorrect behavior. Further information can be found in the Rustc Development Guide.

Additionally, the program will not exit if with_code is called from a spawned thread, unless panics are propagated from that thread. However, propagating panics is usually recommended regardless.


use std::env;

fn read_args() {
    if env::args_os().nth(1).is_some() {
        eprintln!("too many arguments");

fn main() {


  • Cleanly exits the program with an exit code.

Attribute Macros

  • Modifies the main function to exit with the code passed to with_code.