pub struct ExitCode(/* private fields */);
std
only.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()
). 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 main() -> ExitCode {
if !check_foo() {
return ExitCode::from(42);
}
ExitCode::SUCCESS
}
Implementations§
1.61.0 · Source§impl ExitCode
impl ExitCode
1.61.0 · Sourcepub const SUCCESS: ExitCode
pub const SUCCESS: ExitCode
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.
1.61.0 · Sourcepub const FAILURE: ExitCode
pub const FAILURE: ExitCode
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) -> !
🔬This is a nightly-only experimental API. (exitcode_exit_method
)
pub fn exit_process(self) -> !
exitcode_exit_method
)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()
}
Trait Implementations§
Source§impl ConstDefault for ExitCode
impl ConstDefault for ExitCode
1.75.0 · Source§impl Default for ExitCode
The default value is ExitCode::SUCCESS
impl Default for ExitCode
The default value is ExitCode::SUCCESS
1.61.0 · Source§impl Termination for ExitCode
impl Termination for ExitCode
impl Copy for ExitCode
impl StructuralPartialEq for ExitCode
Auto Trait Implementations§
impl Freeze for ExitCode
impl RefUnwindSafe for ExitCode
impl Send for ExitCode
impl Sync for ExitCode
impl Unpin for ExitCode
impl UnwindSafe for ExitCode
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize ⓘ
fn byte_align(&self) -> usize ⓘ
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of_val(&self) -> usize ⓘ
fn mem_align_of_val(&self) -> usize ⓘ
Source§fn mem_size_of_val(&self) -> usize ⓘ
fn mem_size_of_val(&self) -> usize ⓘ
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out
indicating that a T
is niched.