From 988a7e713191fc1633d307d804ec7f126321e8b0 Mon Sep 17 00:00:00 2001 From: Boyd Kane <33420535+beyarkay@users.noreply.github.com> Date: Wed, 13 Jul 2022 18:02:43 +0200 Subject: [PATCH] docs: show how to stringify the output of Comamnd 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, 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`. 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 --- library/std/src/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index d6cba7e7598f6..7874cd0c0757b 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -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