How can I Create a &str String Slice from an Iterator Without Allocating new Strings?

50 points SeaInformation8764 20 comments

Specifically, if I have a std::str::Chars iterator, how can I get a string slice using a method like .collect().

eg.

let chars = "12345 lalala".chars();
// something like this
let number_part: &str = chars.take_while(|&c| c.is_whitespace()).collect();

Or is this something you would need custom iterator functionality for?