[−][src]Function tao_of_rust::ch01::fly_dyn
pub fn fly_dyn(s: &dyn Fly) -> bool
零成本抽象 fly_dyn
struct Duck; struct Pig; trait Fly { fn fly(&self) -> bool; } impl Fly for Duck { fn fly(&self) -> bool { return true; } } impl Fly for Pig { fn fly(&self) -> bool { return false; } } fn fly_static<T: Fly>(s: T) -> bool { s.fly() } fn fly_dyn(s: &Fly) -> bool { s.fly() } assert_eq!(fly_dyn(&Pig), false); assert_eq!(fly_dyn(&Duck), true);Run