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 forVec<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