I would like to implement IntoIterator using a map over an iterator, like this:
struct MyStruct {
txt: String,
}
impl IntoIterator for MyStruct {
type Item = String;
type IntoIter = std::iter::Map<std::ops::Range<char>, WhatAmISupposedToWriteHere>;
fn into_iter(self) -> Self::IntoIter {
('a'..'z').into_iter().map(move |c| {
let mut t = self.txt.clone();
t.push(c);
t
})
}
}
What is the type of my |c| self.txt.clone().push(c) closure that I'd have to write into the IntoIter type?
- The IDE says the type is
[closure@src/example.rs:16:36: 16:64]which isn't very useful. - I tried
dyn Fn(char) -> Stringbut then the compiler complains thatIntoIterdoesn't have a size known at compile-time. - I also tried just writing
_instead of a specific type, but an_is not allowed in a type signature - although it looks like the compiler can definitely find out the type on its own, so it would be nice if it would just magically defineIntoIterfor me.
What is the correct way of using map when implementing IntoIterator?