I'm wondering whether it's possible to avoid small allocations when map produces more outputs than inputs. Here, I'm iterating over bytes one at a time and am producing two bytes during one of the steps.
fn binary_to_hex<'collection, T: IntoIterator<Item = &'collection u8>>(binary: T) -> String {
const HEX_DIGITS: &'static [u8] = b"0123456789ABCDEF";
let hex: Vec<u8> = binary
.into_iter()
.flat_map(|byte| [byte >> 4, byte & 0xF].to_vec()) // can I remove this to_vec() call?
.map(|nibble| HEX_DIGITS[nibble as usize] )
.collect();
// safe because it is only made from HEX_DIGITS
unsafe {
String::from_utf8_unchecked(hex)
}
}
fn main() {
let mess = binary_to_hex(b"\x12\xF9\x13\x02\xA9\x00\x91\x02\x23");
println!("{:?}", mess);
}