use std::fmt; use itertools::{Itertools, Position}; pub struct PrintList<'a, I: Iterator + Clone, E: fmt::Display>(pub I, pub &'a str); impl<'a, I: Iterator + Clone, E: fmt::Display> fmt::Display for PrintList<'a, I, E> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { for (pos, item) in self.0.clone().with_position() { match pos { Position::First | Position::Only => write!(f, "{item}")?, Position::Middle => write!(f, ", {item}")?, Position::Last => write!(f, ", {} {item}", self.1)?, } } Ok(()) } } pub trait IteratorPrint: Iterator + Clone { /// Pretty-print a list with a comma-separated enumeration and an operator /// word (such as "and" or "or") inserted before the last fn display<'a>(self, operator: &'a str) -> PrintList<'a, Self, Self::Item> { PrintList(self, operator) } } impl + Clone> IteratorPrint for T {}