[−][src]Function tao_of_rust::ch01::fly_static
pub fn fly_static<T: Fly>(s: T) -> bool
零成本抽象 fly_static
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() } let pig = Pig; assert_eq!(fly_static::<Pig>(pig), false); let duck = Duck; assert_eq!(fly_static::<Duck>(duck), true);Run