How do I convert an Iterator
<&str>
to a String
, interspersed with a constant string such as "\n"
?
For instance, given:
let xs = vec!["first", "second", "third"];
let it = xs.iter();
One may produce a string s
by collect
ing into a Vec<&str>
and join
ing the result:
let s = it
.map(|&x| x)
.collect::<Vec<&str>>()
.join("\n");
However, this unnecessarily allocates memory for a Vec<&str>
.
Is there a more direct method?