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 1/3] 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 From edd621154a8d0b914337824790419f2a1760d104 Mon Sep 17 00:00:00 2001 From: Boyd Kane <33420535+beyarkay@users.noreply.github.com> Date: Sun, 11 Sep 2022 09:59:04 +0200 Subject: [PATCH 2/3] Use os-specific assertions for os-specific output --- library/std/src/process.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 7874cd0c0757b..d825afe56556a 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -466,15 +466,16 @@ impl fmt::Debug for ChildStderr { /// .args(["/C", "echo hello"]) /// .output() /// .expect("failed to execute process") +/// assert_eq!(String::from_utf8_lossy(&output.stdout), "hello\r\n"); /// } else { /// Command::new("sh") /// .arg("-c") /// .arg("echo hello") /// .output() /// .expect("failed to execute process") +/// assert_eq!(String::from_utf8_lossy(&output.stdout), "hello\n"); /// }; /// -/// assert_eq!(String::from_utf8_lossy(&output.stdout), "hello\n"); /// ``` /// /// `Command` can be reused to spawn multiple processes. The builder methods From 5056eef6ac1fa1ed19140cc6eedce7afc84b3783 Mon Sep 17 00:00:00 2001 From: Boyd Kane <33420535+beyarkay@users.noreply.github.com> Date: Sun, 11 Sep 2022 10:37:44 +0200 Subject: [PATCH 3/3] Typo --- library/std/src/process.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 89219704de635..21243593d3b81 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -461,18 +461,18 @@ impl fmt::Debug for ChildStderr { /// ``` /// use std::process::Command; /// -/// let output = if cfg!(target_os = "windows") { -/// Command::new("cmd") +/// if cfg!(target_os = "windows") { +/// let output = Command::new("cmd") /// .args(["/C", "echo hello"]) /// .output() -/// .expect("failed to execute process") +/// .expect("failed to execute process"); /// assert_eq!(String::from_utf8_lossy(&output.stdout), "hello\r\n"); /// } else { -/// Command::new("sh") +/// let output = Command::new("sh") /// .arg("-c") /// .arg("echo hello") /// .output() -/// .expect("failed to execute process") +/// .expect("failed to execute process"); /// assert_eq!(String::from_utf8_lossy(&output.stdout), "hello\n"); /// }; ///