SHARE
TWEET

Untitled

a guest Jul 17th, 2019 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type Amount = f64;
  2.  
  3. trait TObj {
  4.     fn get_name(&self) -> &str;
  5.     fn get_price(&self) -> Amount;
  6.     fn get_qty(&self) -> Amount;
  7.  
  8.     fn set_qty(&mut self, v: Amount);
  9.  
  10.     fn amount(&self) -> Amount { self.get_price() * self.get_qty() }
  11. }
  12.  
  13. trait TObjTax: TObj {
  14.     fn get_taxrate(&self) -> Amount;
  15.  
  16.     fn tax(&self) -> Amount { self.amount() * self.get_taxrate() / 100. }
  17.  
  18.     fn prn(&self) { println!("{} - amount: {}, tax: {}", self.get_name(), self.amount(), self.tax()); }
  19. }
  20.  
  21. struct Obj {
  22.     name: String,
  23.     price: Amount,
  24.     qty: Amount,
  25.     taxrate: Amount,
  26. }
  27.  
  28. impl TObj for Obj {
  29.     fn get_name(&self) -> &str { &self.name }
  30.     fn get_price(&self) -> Amount { self.price }
  31.     fn get_qty(&self) -> Amount { self.qty }
  32.  
  33.     fn set_qty(&mut self, v: Amount) { self.qty = v; }
  34. }
  35.  
  36. impl TObjTax for Obj {
  37.     fn get_taxrate(&self) -> Amount { self.taxrate }
  38. }
  39.  
  40. fn main() {
  41.     let mut obj = Obj {
  42.         name: "Веник электрический".to_string(),
  43.         price: 12.3,
  44.         qty: 10.,
  45.         taxrate: 18.,
  46.     };
  47.     obj.prn();
  48.  
  49.     obj.set_qty(50.);
  50.     obj.prn();
  51.  
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top