I am learning Rust and discovered this problem:
I would like to split a string by a pattern and remove all cases where the resultin substring is empty.
Here is an example:
let s = "s,o,m,e,";
for elem in s.split(",").skip_while(|&x| x.is_empty()) {
print!(" <{}> ", elem);
//print!(" <{}>({}) ", elem, elem.is_empty());
}
But the result is the following:
<s> <o> <m> <e> <>
My thoughts were: The struct Split returned by split implements Iterator which provides skip_while. IntelliSense told me the x in the closure is of type &&str so I would assume all the elements of the iterator (of type &str) which are empty to be omitted.
But it doesn't skip the empty substring.
I also tried to print the result of the is_empty function. It shows that the last slice is indeed empty. If I instead for the skip_while use skip_while(|&x| x == "s"), it correctly leaves out the "s" (printed with is_empty here):
<o>(false) <m>(false) <e>(false) <>(true)
So somehow the slice behaves differently in the iterator?
Why is that or where am I mistaken?