Is there a more elegant way of chunking an array and then joining it?
fn main() {
let size = 4;
let buffer = vec![0; size * size];
let pbm = format!(
"P4\n{} {}\n{}",
size,
size,
buffer
.chunks(size)
.map(|chunk| chunk
.into_iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(" "))
.collect::<Vec<String>>()
.join("\n")
);
println!("{:?}", pbm);
}
Why do I have to precise type to collect
? Shouldn't it find it out by at what map
returns?