This code:
trait A {}
trait B: A {}
struct S;
impl A for S {}
impl B for S {}
fn main() {
let s = S;
let trait_obj_b: &B = &s;
let trait_obj_a: &A = trait_obj_b;
}
fails with the error:
error[E0308]: mismatched types
--> src/main.rs:14:27
|
14 | let trait_obj_a: &A = trait_obj_b;
| ^^^^^^^^^^^ expected trait `A`, found trait `B`
|
= note: expected type `&A`
found type `&B`
Why? Since B
requires A
, shouldn't all trait objects &B
automatically implement &A
? Is there a way to convert &B
to &A
without changing trait definitions or implementations?