For a practical example of where these methods would be useful, consider this:
// Say the input is expected to look like this:
//
// 123
// 542 13
// 1 23 1145
//
// i.e space-separated number sequences, one per line.
// and the corresponding output must be the Vec [123, 542, 13, 1, 23, 1145]
std::io::stdin().lock()
.lines()
.flat_map(|line| {
let line = line.unwrap();
line.split(' ')
.map(|num| num.parse::<u32>().unwrap())
})
.collect::<Vec<_>>();
The intent of the above code is hopefully clear. It iterates over lines, for each line it iterates over its parts, and ultimately wants to collect them all into a single vector.
Unfortunately, it doesn't compile. This is because line
is a String
, owned by the closure passed to flat_map
. Since the iterator created by split
only borrows the string, line
ends up being dropped at the end of the iteration, which makes the attempt to return the iterator a lifetime error.
It would be perfectly fine for the iterator to keep the string alive after the closure ends. This could easily be achieved by taking ownership of it, instead of simply borrowing
Would it be possible to add into_split
, into_chars
, into_lines
(and similar methods for all the str
methods that return borrowing iterators) to String
s? The idea is that the above snippet would compile if line.split
was replaced with line.into_split