Skip to content

std: add Output::exit_ok #139554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Loading