I know I am probably missing a very important concept here but until now I never had any problems with generics or traits implementations... maybe I was just lucky. Anyway, here is the (somewhat simplified) code:
trait Get {
type Item;
fn get(&self) -> Self::Item;
}
struct Bag<'a, C> {
item: &'a C,
}
impl<'a, C> Get for Bag<'a, C> {
type Item = &'a C;
fn get(&self) -> Self::Item {
self.item
}
}
struct X<C> {
container: C,
}
impl<'a, C> X<C>
where
C: Get<Item = &'a str> + Copy,
{
fn from_bag(bag: Bag<'a, C>) -> Self {
Self { container: bag }
}
}
Can anybody explain why I cannot assign bag
to the container
field even if Bag
implements Get
for any possible C
? The exact compiler error is:
error[E0308]: mismatched types
--> sandbox/src/main.rs:28:27
|
23 | impl<'a, C> X<C>
| - expected this type parameter
...
28 | Self { container: bag }
| ^^^ expected type parameter `C`, found `Bag<'_, C>`
|
= note: expected type parameter `C`
found struct `Bag<'a, C>`