Untitled
unknown
rust
3 years ago
2.3 kB
6
Indexable
use std::fmt;
use std::fmt::Error;
use std::fmt::Formatter;
pub trait Display {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>;
}
#[derive(Copy, Clone, Debug)]
pub struct GPS{
lat: f64,
long: f64,
}
struct Gamer{
name: String,
age: u8,
location: GPS,
equipment: Vec<String>
}
struct Pilot{
name: String,
age: u8,
location: GPS,
flight: Vec<String>
}
struct Doctor{
name: String,
age: u8,
location: GPS,
case:Vec<String>
}
pub trait Employee {
fn get_name(&self) -> String;
fn get_age(&self) -> u8;
fn get_location(&self) -> GPS;
fn get_equipment(&self) -> Vec<String>;
fn get_flight(&self) -> Vec<String>;
fn get_case(&self)->Vec<String>;
}
impl fmt::Display for Gamer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"Gamer: {}\n age: {}\n location: {:?}\n equipment: {:?}",self.name, self.age, self.location, self.equipment)
}
}
impl fmt::Display for Pilot {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"Pilot: {}\n age: {}\n location: {:?}\n flight:{:?}",self.name, self.age, self.location,self.flight)
}
}
impl fmt::Display for Doctor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"Doctor: {}\n age: {}\n location: {:?}\n case: {:?}",self.name, self.age, self.location, self.case)
}
}
fn main() {
let g = Gamer {
name:String::from("JohnOlsen"),
age: 23,
location: GPS {
lat: -100.45675,
long: 14.34567,
},
equipment: vec![String::from("Keyboard"), String::from("Mouse")],
};
let p = Pilot {
name:String::from("Laly"),
age: 45,
location: GPS {
lat: 75.856,
long: -123.643,
},
flight: vec![String::from("Thailand to Japan"), String::from("USA to France")],
};
let d = Doctor {
name:String::from("Steve Hover"),
age: 34,
location: GPS {
lat: 34.654,
long: 90.34567,
},
case: vec![String::from("143"), String::from("Heart Operation")],
};
println!("G = {}", g);
println!("P = {}", p);
println!("D = {}", d);
}
Editor is loading...