1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| trait GetName { fn get_name(&self) -> &String; } trait GetAge { fn get_age(&self) -> u32; } struct PeopleMatchInformation<T,U> { master: T, employee: U, } impl<T:GetName+GetAge,U:GetName+GetAge> PeopleMatchInformation<T,U> { fn print_all_information (&self) { println!("master age = {}",self.master.get_age()); println!("master name = {}",self.master.get_name()); } } struct Teacher { name: String, age: u32, } impl GetName for Teacher { fn get_name(&self) -> &String { &(self.name) } } impl GetAge for Teacher { fn get_age(&self) -> u32 { self.age } } struct Student { name: String, age: u32, } impl GetName for Student { fn get_name(&self) -> &String { &(self.name) } } impl GetAge for Student { fn get_age(&self) -> u32 { self.age } }
fn main() { let s = Student{name:"xiaohuang".to_string(), age: 15,}; let g = Teacher{name:"xiaoming".to_string(), age: 35,}; let m = PeopleMatchInformation{ master: s, employee: g, } m.print_all_information(); }
|