[−][src]Trait tao_of_rust::ch02::generics_trait::Fly
trait示例
Basic usage:
pub trait Fly { fn fly(&self) -> bool; } struct Duck; struct Pig; 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); assert_eq!(fly_dyn(&Pig), false); assert_eq!(fly_dyn(&Duck), true);Run