Rust: How to print Command Output

I was going thru struct Command in std::process 1

Obviously, I tried the very first code snippet in the rust playground. There is a very convinient ▸ to run the sample code.

The code ran without any errors.

But no output 🤔

Ohh, there is no println!. That is why.

No, that was not it. (I mean it was, but it didn’t help.) Adding println!("{:?}", hello); produced this output

[104, 101, 108, 108, 111, 10]

Earlier, without :? - rust compilter told me

the trait std::fmt::Display is not implemented for Vec<u8>

The output is a vector (array) of u8 2

I kept on reading the doc, and later came across io::stdout().write_all 3

So in order to see the output of the command, I had to add use at the top and replace let hello = .. with write_all

use std::io::{self, Write};
...
..
io::stdout().write_all(&output.stdout);

Final code looks like this


  1. See doc ↩︎

  2. As you can see here it is indeed hello represented as numbers. h is 104 and so on. Ending with 10 which is line feed. ↩︎

  3. There is one for stderr as well. ↩︎