I am trying to create an iterator that wraps another iterator with a given associated type and does some additional work. I'm using generic types because it's not pretty to write out the concrete type of the wrapped iterator. I've reduced the error to the following code.
trait Trait {
fn doit(&self);
}
struct S1 {}
impl Trait for S1 {
fn doit(&self) {}
}
fn create_s1() -> S1 {
S1 {}
}
fn create<S>() -> S
where
S: Trait,
{
create_s1()
}
fn main() {
let s = create::<S1>();
}
The error I get is:
error[E0308]: mismatched types
--> src/main.rs:18:5
|
14 | fn create<S>() -> S
| - expected `S` because of return type
...
18 | create_s1()
| ^^^^^^^^^^^ expected type parameter, found struct `S1`
|
= note: expected type `S`
found type `S1`
It seems clear to me that create_s1()
returns type S1
and that type S1
implements Trait
. Why can't the compiler figure out the type of S
?
I'm using rustc 1.32.0-nightly (b3af09205 2018-12-04)
()` means that the caller of `create` can pick whatever concrete type for `S` that they'd like, but your function is only capable of returning a specific concrete type.– Shepmaster Dec 06 '18 at 01:10