Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Post Reopened by Ivan Gabriele, Mateen Ulhaq, Matt Joiner
Add links. Make difference versus duplicate question clearer -- to avoid allocating memory for an intermediate Vec when joining an Iterator of &str.
Source Link
Mateen Ulhaq

How do I convert an Iterator<&str>Iterator<&str> to a StringString, 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 collectcollecting into somea IterableVec<&str> and joinjoining the result:

let s = it
    .map(|&x| x)
    .collect::<Vec<&str>>()
    .join("\n");

However, this unnecessarily allocates memorythis unnecessarily allocates memory for a Vec<&str>.

Is there a more direct method?

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 collecting into some Iterable and joining 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?

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 collecting into a Vec<&str> and joining 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?

Shorten. Make difference versus duplicate question clearer -- to avoid allocating memory for an intermediate Vec when joining an Iterator of &str.
Source Link
Mateen Ulhaq

What is the canonical method toHow do I convert an Iterator<&str> to a String, interspersed with somea constant string (e.g.such as "\n")?

For For instance, given:

let xs = vec!["first", "second", "third"];
let it = xs.iter();

There is a way toOne may produce a string s by collectingcollecting into some Iterable and joining 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?

What is the canonical method to convert an Iterator<&str> to a String, interspersed with some constant string (e.g. "\n")?

For instance, given:

let xs = vec!["first", "second", "third"];
let it = xs.iter();

There is a way to produce a string s by collecting into some Iterable and joining 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?

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 collecting into some Iterable and joining 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?

Post Closed as "Duplicate" by Stargateur, Shepmaster rust
added 22 characters in body
Source Link
Mateen Ulhaq

What is the canonical method to convert an Iterator<&str> to a String, interspersed with some constant string (e.g. "\n")?

For instance, given:

let xs = vec!["first", "second", "third"];
let it = xs.iter();

There is a way to produce a string s by collecting into some Iterable and joining 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?

What is the canonical method to convert an Iterator<&str> to a String, interspersed with some constant string (e.g. "\n")?

For instance, given:

let xs = vec!["first", "second", "third"];
let it = xs.iter();

There is a way to produce a string s by collecting into some Iterable and joining the result:

let s = it
    .collect::<Vec<&str>>()
    .join("\n");

However, this unnecessarily allocates memory for a Vec<&str>. Is there a more direct method?

What is the canonical method to convert an Iterator<&str> to a String, interspersed with some constant string (e.g. "\n")?

For instance, given:

let xs = vec!["first", "second", "third"];
let it = xs.iter();

There is a way to produce a string s by collecting into some Iterable and joining 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?

Source Link
Mateen Ulhaq
Loading
lang-rust