pub struct ExitCode(_);
Expand description
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()
), and intentionally does not provide accessors like
PartialEq
, Eq
, or Hash
. Instead 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 main() -> ExitCode {
if !check_foo() {
return ExitCode::from(42);
}
ExitCode::SUCCESS
}
RunImplementations
sourceimpl ExitCode
impl ExitCode
sourcepub const SUCCESS: ExitCode = ExitCode(imp::ExitCode::SUCCESS)
pub const SUCCESS: ExitCode = ExitCode(imp::ExitCode::SUCCESS)
The canonical ExitCode
for successful termination on this platform.
Note that a ()
-returning main
implicitly results in a successful
termination, so there’s no need to return this from main
unless
you’re also returning other possible codes.
sourcepub const FAILURE: ExitCode = ExitCode(imp::ExitCode::FAILURE)
pub const FAILURE: ExitCode = ExitCode(imp::ExitCode::FAILURE)
The canonical ExitCode
for unsuccessful termination on this platform.
If you’re only returning this and SUCCESS
from main
, consider
instead returning Err(_)
and Ok(())
respectively, which will
return the same codes (but will also eprintln!
the error).
sourcepub fn exit_process(self) -> !
pub fn exit_process(self) -> !
Exit the current process with the given ExitCode
.
Note that this has the same caveats as process::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. 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). ExitCode
exists because of this; only
ExitCode
s 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)]
// 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()
}
RunTrait Implementations
sourceimpl Termination for ExitCode
impl Termination for ExitCode
impl Copy for ExitCode
Auto Trait Implementations
impl RefUnwindSafe for ExitCode
impl Send for ExitCode
impl Sync for ExitCode
impl Unpin for ExitCode
impl UnwindSafe for ExitCode
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more