1#[cfg(feature = "alloc")]
2use core::fmt;
3
4#[cfg(feature = "alloc")]
5use alloc::string::String;
6
7pub trait IteratorExt: Iterator {
8 #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
9 #[cfg(feature = "alloc")]
10 fn join_(&mut self, sep: &str) -> String
11 where
12 Self::Item: fmt::Display,
13 {
14 use core::fmt::Write as _;
15
16 match self.next() {
17 None => String::new(),
18 Some(first) => {
19 let (lower, _) = self.size_hint();
20 let mut buf = String::with_capacity(sep.len() * lower);
21 write!(&mut buf, "{first}").unwrap();
22 self.for_each(|item| {
23 buf.push_str(sep);
24 write!(&mut buf, "{item}").unwrap();
25 });
26 buf
27 }
28 }
29 }
30}
31
32impl<I: Iterator> IteratorExt for I {}