- Suppose
it
is an iterator over String or &str, we can do something like:
let ans: Vec<String> = ans.collect();
ans.join(" ");
- Now, I'm wondering if it's possible to
join
on the iterator directly, without first collecting it into aVec<String>
it
is an iterator over String or &str, we can do something like:let ans: Vec<String> = ans.collect();
ans.join(" ");
join
on the iterator directly, without first collecting it into a Vec<String>
You can with itertools:
Note that join
on slices is more efficient than Itertools::join
, because it can pre-calculate the amount of memory that the final result will need and forgo bounds checking in the copy loop as a result. You can't do that with an Iterator
, which is why the standard library's join
is defined only for slices and not for any iterator.