Hello, I'm facing an issue with None
and the IntoIterator
of another type.
struct MyStruct {}
fn test<I, T>(iter: I)
where
I: IntoIterator<Item = T>,
T: Into<MyStruct>,
{
todo!();
}
fn main() {
test(vec![MyStruct {}, MyStruct {}]);
test(None); // Error !
}
Playground: Rust Playground.
The previous code give the following error:
error[E0283]: type annotations needed
--> src/main.rs:13:5
|
3 | fn test<I, T>(iter: I)
| ---- required by a bound in this
...
6 | T: Into<MyStruct>,
| -------------- required by this bound in `test`
...
13 | test(None);
| ^^^^ cannot infer type for type parameter `T` declared on the function `test`
|
= note: cannot satisfy `_: Into<MyStruct>`
help: consider specifying the type arguments in the function call
|
13 | test::<I, T>(None);
| ^^^^^^^^
Which I understand, I must type the option since Rust can't infer the appropriate type, so doing the following: test(Option::<MyStruct>::None)
works. Playground fixed: Rust Playground.
But for the sake of simplicity I would like the user of my API to only write None
. Is it possible?
Thanks for the help.