I feel I got to the border of my ignorance.
I want to replace a character with a few characters inside of iterator. (I know there are many other ways to do it, but I want to clear this bit of my ignorance).
I decided to use flat_map
and to convert each character into iterator, which is either sequence of characters, or 'one' character.
If it's a dot (.
), make it a sequence of [
, .
, ]
:
'.' => "[.]".chars()
As far as I understand, it's returning iterator over str
, which will return those characters.
For every other character I want to return it 'as is'. Because they are in the in the same match
, I need to make an iterator with a single value:
c => iter::once(c)
Now, when I combine it all together:
pub fn myfunc(input: String) -> String {
input.chars().flat_map(
|c|{
match c{
'.' => "[.]".chars(),
c => iter::once(c)
}
}
).collect()
}
But it does not work (if it would, I wouldn't be here...): expected struct 'Chars', found struct 'std::iter::Once'
. It's two different structures, both implementing iterator protocol.
Here is my ignorance. I feel here should be some kind of magic to make both iterators getting the same type (having the same trait?), but I don't know how it should be.
What is in the Rust to make those two iterators compatible?