-
Notifications
You must be signed in to change notification settings - Fork 13.3k
docs: show how to stringify the output of Command #99214
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
Conversation
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
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
r? @thomcc (rust-highfive has picked a reviewer for you, use r? to override) |
Rather than calling lossy string conversions, how about adding an example to the documentation showing how to print the error output to stderr without converting it? That just requires a call to write_all. |
Happy to remove the lossy conversion, although I initially favoured using the I haven't used #![allow(unused)]
use std::io::{self, Stdout, Write};
use std::process::Command;
fn main() -> io::Result<()> {
let output = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(["/C", "echo hello"])
.output()
.expect("failed to execute process")
} else {
Command::new("sh")
.arg("-c")
.arg("echo hello")
.output()
.expect("failed to execute process")
};
// Note that `output.stdout` is a slice of bytes
assert_eq!(&output.stdout, b"hello\n");
// It can be written to stdout as a string either via stdout.write_all:
let mut stdout = io::stdout().lock();
stdout.write_all(&output.stdout)?;
// Or via a lossy conversion:
assert_eq!(String::from_utf8_lossy(&output.stdout), "hello\n");
Ok(())
} Possibly excluding the Thanks for the help! |
@rustbot label +S-waiting-on-review -S-waiting-on-author |
IIRC that fails on windows if it's not UTF-8, FWIW. Although I guess you probably wouldn't get UTF-8 input there (although you could from a program with a different codepage, presumably). Anyway, I've done or seen the lossy converstion dance here a jillion times and can't think of a case where I've seen the direct logging to stderr, so I think this is fine as is. @bors r+ rollup |
Thanks!
Didn't know that, good to know 🤓 |
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
also failed earlier here: #99439 (comment) |
So based on rust-log-analyzer:
It seems like the failure was at std/process:19 which wasn't modified in my commit, although I do include a similar assertion. Happy to modify my changes, but I'm not sure if they were the culprit? |
Sorry, still getting used to the rust PR system. Is there anything I need to do to make this commit merge-able? Thanks! |
It failed in CI when merging, you need to make sure it passes the tests that failed. |
@beyarkay FYI: when a PR is ready for review, send a message containing |
Okay I've hopefully fixed the issue causing the assertions to sometimes fail. My knowledge of OS-specific line endings isn't great, but it looks like on some builds the assertion would fail because I used Side note, I got confused by this line (reproduced below):
Because it reads to me like the error was on line 19 (which made me think it wasn't something I'd caused) but on closer reading it's referring to line 461 which was the issue I'd caused. I also see that line 19 is the last line of the first example code block in @rustbot label +S-waiting-on-review -S-waiting-on-author |
This comment has been minimized.
This comment has been minimized.
This looks fine, but you have a merge commit in the history, which we don't allow (See https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy). Can you clean up the git history? |
triage: |
This documentation-only change clarifies how the user can get the string representation of the output of a command.
The
stdout
andstderr
members ofstd::process::Output
fromstd::process::Command
return a Vec, which is not always what the user wants.For simple cases like printing
stderr
iff thestd::process::ExitStatus
is non-zero, it's useful to get the string representation of thisVec<u8>
. This can be done viaString::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