I would like to know which is the most idiomatic Rust way to print the elements in a vector in a contigously manner. For example, in the following code:
fn main() {
let vector = vec![0x54, 0xaf, 0x5c];
println!("{:2x?}", vector);
}
I would like to print: 54af5c and not [54, af, 5c].
>Solution :
If you want to do this in one line without multiple calls to the print! macro you could do it like so:
fn main() {
let vector = vec![0x54, 0xaf, 0x5c];
println!("{}", vector.iter().map(|n| format!("{:x}", n)).fold(String::new(), |acc, arg| acc + arg.as_str()));
}
Here is the Playground.
This also has the added benefit, that there can be differently formatted hexadecimal numbers in your vector and you can always format them as either LowerHex or UpperHex.