diff --git a/library/std/src/process.rs b/library/std/src/process.rs index bf2a5088a1c..76fb92e5423 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -1741,6 +1741,29 @@ impl ExitCode { /// behavior][exit#platform-specific-behavior]). `ExitCode` exists because of this; only /// `ExitCode`s that are valid on all platforms can be created, so those problems don't exist /// 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() + /// } + /// ``` #[unstable(feature = "exitcode_exit_method", issue = "none")] pub fn exit_process(self) -> ! { exit(self.to_i32())