In the following code I have a simple trait, A, and a struct Foo that implements A...
Next, I define a function that takes a reference to a trait object. From main() I pass in a reference to a concrete Foo. This works fine and successfully calls the method in the trait.
trait A {
fn do_something(&self) {
println!("Doing something.");
}
}
struct Foo {}
impl A for Foo {}
fn main() {
let foo = Foo {};
make_do_something(&foo);
}
fn make_do_something(a: &dyn A) {
a.do_something();
}
What is the Rust compiler doing here? Is the concrete Foo reference automatically coerced to a Trait object reference? That is, is a new trait object being created on the heap that is referring to my concrete object? I can't find a clear description of how this works in the documentation.