I'd like to nominate literator for crate of the week. A crate for Displaying iterator of things.
In my Rust career, I wrote a lot of custom Display just so I could print out contents of a list with a nice comma-delimited format.
I just found this crate while thinking "but someone has to have written a crate for that already" and searching on lib.rs. And it turns out it does! This crate encapsulates all that mess, and I I'll never have to write the following ever again
:
if let Some(first) = iter.next() {
write!(f, "{first}")?;
}
let but_last = iter.len().saturating_sub(1);
for elem in iter.by_ref().take(but_last) {
write!(f, ", {elem}")?;
}
if let Some(last) = iter.next() {
write!(f, ", and {last}")?;
}
I can now just do:
write!(f, "{}", iter.join_comma_and())