about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNoa <coolreader18@gmail.com>2022-05-10 21:56:20 -0500
committerNoa <coolreader18@gmail.com>2022-05-13 13:28:22 -0500
commit688dcc68fe2a4bcd781c24b36cfd101fb20a5a50 (patch)
tree364ea8904bd135ee93daeff8cd6ce6765874791b
parent0df02bb35b8719d32ac48de6bc72e1cec42646ba (diff)
downloadrust-688dcc68fe2a4bcd781c24b36cfd101fb20a5a50.tar.gz
rust-688dcc68fe2a4bcd781c24b36cfd101fb20a5a50.zip
Add ExitCode::exit_process example
-rw-r--r--library/std/src/process.rs23
1 files changed, 23 insertions, 0 deletions
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())