Struct ExitCode

struct ExitCode(_)

This type represents the status code the current process can return to its parent under normal termination.

ExitCode is intended to be consumed only by the standard library (via [Termination::report()]). For forwards compatibility with potentially unusual targets, this type currently does not provide Eq, Hash, or access to the raw value. This type does provide PartialEq for comparison, but note that there may potentially be multiple failure codes, some of which will not compare equal to ExitCode::FAILURE. The standard library provides the canonical SUCCESS and FAILURE exit codes as well as From<u8> for ExitCode for constructing other arbitrary exit codes.

Portability

Numeric values used in this type don't have portable meanings, and different platforms may mask different amounts of them.

For the platform's canonical successful and unsuccessful codes, see the SUCCESS and FAILURE associated items.

Differences from ExitStatus

ExitCode is intended for terminating the currently running process, via the Termination trait, in contrast to ExitStatus, which represents the termination of a child process. These APIs are separate due to platform compatibility differences and their expected usage; it is not generally possible to exactly reproduce an ExitStatus from a child for the current process after the fact.

Examples

ExitCode can be returned from the main function of a crate, as it implements [Termination]:

use std::process::ExitCode;
# fn check_foo() -> bool { true }

fn main() -> ExitCode {
    if !check_foo() {
        return ExitCode::from(42);
    }

    ExitCode::SUCCESS
}

Implementations

impl ExitCode

fn exit_process(self: Self) -> never

Exit the current process with the given ExitCode.

Note that this has the same caveats as [process::exit()][exit], namely that this function terminates the process immediately, so no destructors on the current stack or any other thread's stack will be run. Also see those docs for some important notes on interop with C code. If a clean shutdown is needed, it is recommended to simply return this ExitCode from the main function, as demonstrated in the type documentation.

Differences from process::exit()

process::exit() accepts any i32 value as the exit code for the process; however, there are platforms that only use a subset of that value (see [process::exit platform-specific behavior][exit#platform-specific-behavior]). ExitCode exists because of this; only ExitCodes that are supported by a majority of our platforms can be created, so those problems don't exist (as much) with this method.

Examples

#![feature(exitcode_exit_method)]
# use std::process::ExitCode;
# use std::fmt;
# enum UhOhError { GenericProblem, Specific, WithCode { exit_code: ExitCode, _x: () } }
# impl fmt::Display for UhOhError {
#     fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { unimplemented!() }
# }
// there's no way to gracefully recover from an UhOhError, so we just
// print a message and exit
fn handle_unrecoverable_error(err: UhOhError) -> ! {
    eprintln!("UH OH! {err}");
    let code = match err {
        UhOhError::GenericProblem => ExitCode::FAILURE,
        UhOhError::Specific => ExitCode::from(3),
        UhOhError::WithCode { exit_code, .. } => exit_code,
    };
    code.exit_process()
}

impl Clone for ExitCode

fn clone(self: &Self) -> ExitCode

impl Copy for ExitCode

impl Debug for ExitCode

fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result

impl Default for ExitCode

fn default() -> Self

impl ExitCodeExt for process::ExitCode

fn from_raw(raw: u32) -> Self

impl Freeze for ExitCode

impl From for ExitCode

fn from(code: u8) -> Self

Constructs an ExitCode from an arbitrary u8 value.

impl PartialEq for ExitCode

fn eq(self: &Self, other: &ExitCode) -> bool

impl RefUnwindSafe for ExitCode

impl Send for ExitCode

impl StructuralPartialEq for ExitCode

impl Sync for ExitCode

impl Termination for ExitCode

fn report(self: Self) -> ExitCode

impl Unpin for ExitCode

impl UnwindSafe for ExitCode

impl<T> Any for ExitCode

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for ExitCode

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for ExitCode

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for ExitCode

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for ExitCode

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for ExitCode

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T, U> Into for ExitCode

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for ExitCode

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for ExitCode

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>