Add ExitCode::exit_process example
This commit is contained in:
parent
0df02bb35b
commit
688dcc68fe
1 changed files with 23 additions and 0 deletions
|
@ -1741,6 +1741,29 @@ impl ExitCode {
|
||||||
/// behavior][exit#platform-specific-behavior]). `ExitCode` exists because of this; only
|
/// 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
|
/// `ExitCode`s that are valid on all platforms can be created, so those problems don't exist
|
||||||
/// with this method.
|
/// 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")]
|
#[unstable(feature = "exitcode_exit_method", issue = "none")]
|
||||||
pub fn exit_process(self) -> ! {
|
pub fn exit_process(self) -> ! {
|
||||||
exit(self.to_i32())
|
exit(self.to_i32())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue