let mut buf = [0;26];
for _ in 0..200{
let bytes_read = stream.read(&mut buf)?;
println!("Bytes read {}", bytes_read);
stream.write(&buf[..bytes_read])?;
**println!("from the sender:{:#?}",buf);**
thread::sleep(time::Duration::from_secs(1));
}
How to print all the 26 bytes as hex in a single line after space?
This now prints as below:
Bytes read 26
from the sender:[
172,
181,
177,
178,
190,
162,
169,
179,
163,
166,
138,
141,
128,
167,
181,
172,
179,
191,
189,
165,
180,
191,
172,
165,
161,
178,
You likely should use print!("{b:02X} ");, otherwise on 0x0F you will get F F F F F. Also I would note that from performance point of view printing hex-encoding of every byte is far from efficient.
There is no practical difference on such toy exercise in the first place. I mentioned it because on less toy problems it may matter.
There are two parts to consider:
Encoding efficiency. println! and format! will be less efficient than encoding into a stack-allocated fixed-size buffer. At the very least the latter removes dynamic dispatch currently used by Rust formatting machinery (it may change in future, but for now it is what it is), which allows compiler to perform various optimizations.
Number of write syscalls. Using one write call is better than many. And with print!ing every byte you also pay unnecessary locking cost.