0

I haven't found why this won't work or what is the proper way to cast this to get it to pass.

I have a trait in an external crate to be executed by a VM (playground):

trait Foo {}

trait Bar {}

trait OtherFoo: Bar {}

impl Foo for dyn OtherFoo {}

fn use_foo(_: &dyn Foo) {}

fn use_other_foo_as_foo(other_foo: &dyn OtherFoo) {
    use_foo(other_foo)
}

I get

error[E0308]: mismatched types
  --> src/lib.rs:12:13
   |
12 |     use_foo(other_foo)
   |             ^^^^^^^^^ expected trait `Foo`, found trait `OtherFoo`
   |
   = note: expected reference `&dyn Foo`
              found reference `&dyn OtherFoo`

I would figure that since it implements the trait that it would be allowed to be passed in but I realize Rust doesn't go by reference and thus a cast of some kind is needed, but I have no idea how to cast this accordingly.

It says I can't use as as that only applies to primitive casts and I haven't been able to find exactly what I'm looking for.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366

0 Answers0