Skip to content

Commit

Permalink
Rollup merge of rust-lang#99214 - beyarkay:patch-1, r=thomcc
Browse files Browse the repository at this point in the history
docs: show how to stringify the output of Command

This documentation-only change clarifies how the user can get the string representation of the output of a command.

The `stdout` and `stderr` members of `std::process::Output` from `std::process::Command` return a Vec<u8>, which is not always what the user wants.

For simple cases like printing `stderr` iff the `std::process::ExitStatus` is non-zero, it's useful to get the string representation of this `Vec<u8>`. This can be done via `String::from_utf8_lossy`, but it's not clear that this is possible from the documentation without first searching the internet for an answer.

Link to playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8189f65dc4b354a643311af2cea5b230
  • Loading branch information
matthiaskrgr authored Jul 18, 2022
2 parents ed91732 + 988a7e7 commit 1abf43b
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ impl fmt::Debug for ChildStderr {
/// .expect("failed to execute process")
/// };
///
/// let hello = output.stdout;
/// assert_eq!(String::from_utf8_lossy(&output.stdout), "hello\n");
/// ```
///
/// `Command` can be reused to spawn multiple processes. The builder methods
Expand Down

0 comments on commit 1abf43b

Please sign in to comment.