Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 688dcc6

Browse files
committed
Add ExitCode::exit_process example
1 parent 0df02bb commit 688dcc6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

library/std/src/process.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,29 @@ impl ExitCode {
17411741
/// behavior][exit#platform-specific-behavior]). `ExitCode` exists because of this; only
17421742
/// `ExitCode`s that are valid on all platforms can be created, so those problems don't exist
17431743
/// with this method.
1744+
///
1745+
/// # Examples
1746+
///
1747+
/// ```
1748+
/// #![feature(exitcode_exit_method)]
1749+
/// # use std::process::ExitCode;
1750+
/// # use std::fmt;
1751+
/// # enum UhOhError { GenericProblem, Specific, WithCode { exit_code: ExitCode, _x: () } }
1752+
/// # impl fmt::Display for UhOhError {
1753+
/// # fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result { unimplemented!() }
1754+
/// # }
1755+
/// // there's no way to gracefully recover from an UhOhError, so we just
1756+
/// // print a message and exit
1757+
/// fn handle_unrecoverable_error(err: UhOhError) -> ! {
1758+
/// eprintln!("UH OH! {err}");
1759+
/// let code = match err {
1760+
/// UhOhError::GenericProblem => ExitCode::FAILURE,
1761+
/// UhOhError::Specific => ExitCode::from(3),
1762+
/// UhOhError::WithCode { exit_code, .. } => exit_code,
1763+
/// };
1764+
/// code.exit_process()
1765+
/// }
1766+
/// ```
17441767
#[unstable(feature = "exitcode_exit_method", issue = "none")]
17451768
pub fn exit_process(self) -> ! {
17461769
exit(self.to_i32())

0 commit comments

Comments
 (0)