Hey, this is my first topic so in addition to being a request for help, see this as a greeting message.
I'm struggling with the some code that looks somewhat like this:
pub struct A<I: Iterator> {
s: I,
}
impl<I: Iterator> A<I> {
pub fn new(s: &str) -> A<impl Iterator<Item = char> + '_> {
A {
s: s.chars()
}
}
}
fn main() {
drop(A::new("").s);
}
As you can see, what I'm trying to achieve is defining A
to be a struct that contains an iterator (in this particular case it would have worked as well either by removing the bounds or by explicating the associated type Item
too). Then I'd use A::new
to instanciate it.
The point is, rustc complains that
error[E0282]: type annotations needed
--> src/main.rs:15:10
|
15 | drop(A::new("").s);
| ^^^^^^ cannot infer type of the type parameter `I` declared on the struct `A`
|
help: consider specifying the generic argument
|
15 | drop(A::<I>::new("").s);
| +++++
For more information about this error, try `rustc --explain E0282`.
Of course, if I were to do what it suggests, everything would work:
// preceding code unchanged
fn main() {
drop(A::<Chars>::new("").s);
}
But my iterator is more complex than a mere Chars
, and I was hoping to remove the need to make the type explicit (since it would be infeasable, or at least, extremely incovenient).
Doesn't the compiler have all it needs to infere the type? Am I doing something wrong? thanks for your help.