Creates a type whose [`fmt::Display`] implementation outputs each item in `iter` separated by `separator`.
(separator: &'a str, iter: I)
| 191 | /// Creates a type whose [`fmt::Display`] implementation outputs each item in |
| 192 | /// `iter` separated by `separator`. |
| 193 | pub fn separated<'a, I>(separator: &'a str, iter: I) -> impl fmt::Display + 'a |
| 194 | where |
| 195 | I: IntoIterator, |
| 196 | I::IntoIter: Clone + 'a, |
| 197 | I::Item: fmt::Display + 'a, |
| 198 | { |
| 199 | struct Separated<'a, I> { |
| 200 | separator: &'a str, |
| 201 | iter: I, |
| 202 | } |
| 203 | |
| 204 | impl<'a, I> fmt::Display for Separated<'a, I> |
| 205 | where |
| 206 | I: Iterator + Clone, |
| 207 | I::Item: fmt::Display, |
| 208 | { |
| 209 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 210 | for (i, item) in self.iter.clone().enumerate() { |
| 211 | if i != 0 { |
| 212 | write!(f, "{}", self.separator)?; |
| 213 | } |
| 214 | write!(f, "{}", item)?; |
| 215 | } |
| 216 | Ok(()) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | Separated { |
| 221 | separator, |
| 222 | iter: iter.into_iter(), |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /// A helper struct to keep track of indentation levels. |
| 227 | /// |
no test coverage detected