Untitled
unknown
rust
3 years ago
2.6 kB
8
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 LatLong {
ns: f64,
ew: f64,
}
impl fmt::Display for LatLong {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut ewc = 'E';
if self.ew < 0.0 { ewc = 'W'; }
let mut nsc = 'N';
if self.ns < 0.0 { nsc = 'S'; }
write!(f, "({}{}, {}{})", self.ew, ewc, self.ns, nsc)
}
}
struct Chelf {
name: String,
age: u8,
menu: Vec<String>,
location: LatLong,
}
struct Teacher {
name: String,
age: u8,
location: LatLong,
class: Vec<String>,
}
struct SoftEng {
name: String,
age: u8,
location: LatLong,
skills: Vec<String>,
}
pub trait Employee {
fn get_name(&self) -> String;
fn get_age(&self) -> u8;
fn get_location(&self) -> LatLong;
fn get_menu(&self) -> Vec<String>;
fn get_skills(&self) -> Vec<String>;
fn get_class(&self)->Vec<String>;
}
impl fmt::Display for Chelf {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"Manager: {}\n age: {}\n location: {}\n menu: {:?}",self.name, self.age, self.location, self.menu)
}
}
impl fmt::Display for Teacher {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"Teacher: {}\n age: {}\n location: {}\n class:{:?}",self.name, self.age, self.location,self.class)
}
}
impl fmt::Display for SoftEng {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f,"SoftEng: {}\n age: {}\n location: {}\n skills: {:?}",self.name, self.age, self.location, self.skills)
}
}
fn main() {
let c = Chelf {
name: String::from("John"),
age: 30,
menu: vec![String::from("Pizza"), String::from("Roti")],
location: LatLong {
ns: 14.34567,
ew: -100.45675,
},
};
let t = Teacher {
name: String::from("Alice"),
age: 20,
class: vec![String::from("Language"), String::from("Math")],
location: LatLong {
ns: 14.34567,
ew: 100.45675,
},
};
let s = SoftEng {
name: String::from("Bob"),
age: 25,
location: LatLong {
ns: 14.34567,
ew: 100.45675,
},
skills: vec![String::from("Rust"), String::from("C++")],
};
println!("m = {}", c);
println!("w = {}", t);
println!("s = {}", s);
}Editor is loading...