[][src]Function tao_of_rust::ch03::abstract_type::trait_object

pub fn trait_object()

抽象类型:Box(装箱)抽象类型 之 trait对象

Base usage:

#[derive(Debug)]
struct Foo;
trait Bar {
    fn baz(&self);
}
impl Bar for Foo {
    fn baz(&self) { println!("{:?}", self) }
}
fn static_dispatch<T>(t: &T) where T:Bar {
    t.baz();
}
fn dynamic_dispatch(t: &Bar) {
    t.baz();
}
let foo = Foo;
static_dispatch(&foo);
dynamic_dispatch(&foo);Run