traits
unknown
rust
3 years ago
2.9 kB
5
Indexable
use std::fmt; #[derive(Copy, Clone, Debug)] struct GPS{ lat: f64, lon: f64, } struct Teacher{ name: String, age: i64, location: GPS, student: Vec<String>, } struct Police{ name: String, age: i64, location: GPS, weapon: Vec<String>, } struct Doctor{ name: String, age: i64, location: GPS, patient: Vec<String>, } trait Employee{ fn find_location(&self) -> GPS; fn find_name(&self) -> &String; fn find_age(&self) -> i64; fn find_properties(&self) -> &Vec<String>; } impl Employee for Teacher{ fn find_location(&self) -> GPS{ self.location } fn find_name(&self) -> &String{ &self.name } fn find_age(&self) -> i64{ self.age } fn find_properties(&self) -> &Vec<String>{ &self.student } } impl Employee for Police{ fn find_location(&self) -> GPS{ self.location } fn find_name(&self) -> &String{ &self.name } fn find_age(&self) -> i64{ self.age } fn find_properties(&self) -> &Vec<String>{ &self.weapon } } impl Employee for Doctor{ fn find_location(&self) -> GPS{ self.location } fn find_name(&self) -> &String{ &self.name } fn find_age(&self) -> i64{ self.age } fn find_properties(&self) -> &Vec<String>{ &self.patient } } impl fmt::Display for Teacher { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, " Name: {}\n Age: {}\n Student: {:?}\n Pos: {}, {}\n", self.find_name(), self.find_age(), self.find_properties(), self.find_location().lat, self.find_location().lon) } } impl fmt::Display for Police { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, " Name: {}\n Age: {}\n Weapon: {:?}\n Pos: {}, {}\n", self.find_name(), self.find_age(), self.find_properties(), self.find_location().lat, self.find_location().lon) } } impl fmt::Display for Doctor { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, " Name: {}\n Age: {}\n Patient: {:?}\n Pos: {}, {}\n", self.find_name(), self.find_age(), self.find_properties(), self.find_location().lat, self.find_location().lon) } } fn main() { let a = Teacher{name: "John".to_string(), age: 65, student: vec![String::from("A1"), String::from("A2")], location: GPS { lat: 8.15154, lon: -584.55 }}; println!("{}", a); let b = Police{name: "John".to_string(), age: 65, weapon: vec![String::from("A1"), String::from("A2")], location: GPS { lat: 8.15154, lon: -584.55 }}; println!("{}", b); let c = Doctor{name: "John".to_string(), age: 65, patient: vec![String::from("A1"), String::from("A2")], location: GPS { lat: 8.15154, lon: -584.55 }}; println!("{}", c); }
Editor is loading...