The idiomatic way of printing slice/array in hexadecimal


#1

As of now (v1.14), what is the idiomatic (and most convenient) way to print/log a byte array/slice like those often needed when working with cryptographic code or networking protocols? Hopefully no function declaring and loops involved like the old answers I have found elsewhere.

let mut ciphertext: [u8; 256] = [0; 256];
println!("RSA private encrypted: {:?}", ciphertext);

I got

the trait `std::fmt::Debug` is not implemented for `[u8; 256]`

Built-in facilities are preferred but good third-party ones are welcome as well.


#2

Itertools has a “format adaptor”

extern crate itertools;
use itertools::Itertools;
let mut ciphertext: [u8; 256] = [0; 256];
println!("RSA private encrypted: {:02x}", ciphertext.iter().format(""));

(playground)

The string argument is the element separator.