diff options
| author | Stuart Cook <Zalathar@users.noreply.github.com> | 2025-04-15 15:47:26 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-15 15:47:26 +1000 |
| commit | 6a9d27d320fcccdedceb9465ed345556ea54db18 (patch) | |
| tree | 704a6af3e5a08eb4cd029b133d1d13966c41110e | |
| parent | 46b197ad3b2173828e453617e15f65edc8dbe07d (diff) | |
| parent | 9676d4aeb7d918179f4b5d39e10ad360ec5b2577 (diff) | |
| download | rust-6a9d27d320fcccdedceb9465ed345556ea54db18.tar.gz rust-6a9d27d320fcccdedceb9465ed345556ea54db18.zip | |
Rollup merge of #139554 - lolbinarycat:std-output-exit_ok, r=tgross35
std: add Output::exit_ok approved in ACP https://github.com/rust-lang/libs-team/issues/554 Tracking issue: https://github.com/rust-lang/rust/issues/84908
| -rw-r--r-- | library/std/src/process.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/library/std/src/process.rs b/library/std/src/process.rs index da518011a0e..76ce7bce81b 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -1286,6 +1286,40 @@ pub struct Output { pub stderr: Vec<u8>, } +impl Output { + /// Returns an error if a nonzero exit status was received. + /// + /// If the [`Command`] exited successfully, + /// `self` is returned. + /// + /// This is equivalent to calling [`exit_ok`](ExitStatus::exit_ok) + /// on [`Output.status`](Output::status). + /// + /// Note that this will throw away the [`Output::stderr`] field in the error case. + /// If the child process outputs useful informantion to stderr, you can: + /// * Use `cmd.stderr(Stdio::inherit())` to forward the + /// stderr child process to the parent's stderr, + /// usually printing it to console where the user can see it. + /// This is usually correct for command-line applications. + /// * Capture `stderr` using a custom error type. + /// This is usually correct for libraries. + /// + /// # Examples + /// + /// ``` + /// #![feature(exit_status_error)] + /// # #[cfg(unix)] { + /// use std::process::Command; + /// assert!(Command::new("false").output().unwrap().exit_ok().is_err()); + /// # } + /// ``` + #[unstable(feature = "exit_status_error", issue = "84908")] + pub fn exit_ok(self) -> Result<Self, ExitStatusError> { + self.status.exit_ok()?; + Ok(self) + } +} + // If either stderr or stdout are valid utf8 strings it prints the valid // strings, otherwise it prints the byte sequence instead #[stable(feature = "process_output_debug", since = "1.7.0")] |
