Skip to content

Commit

Permalink
docs: show how to stringify the output of Comamnd
Browse files Browse the repository at this point in the history
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
beyarkay authored Jul 13, 2022
1 parent ca4e394 commit 988a7e7
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 988a7e7

Please sign in to comment.