Add ExitCode::exit_process example

This commit is contained in:
Noa 2022-05-10 21:56:20 -05:00
parent 0df02bb35b
commit 688dcc68fe
No known key found for this signature in database
GPG Key ID: 7F9F7DB1768C59CF

View File

@ -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())