Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- let t = Thailand {};
- // let u: USA = Box::new();
- //let dyn_tax_calculator = Box::<dyn CompoundTax>::new();
- //let usa = Box::new(USA {
- // tax_calculator: dyn_tax_calculator,
- //});
- //println!(usa.compound_tax(333, 2.3, 3.3));
- }
- struct Thailand {}
- struct USA {
- pub tax_calculator: Box<dyn CompoundTax>,
- }
- trait SimpleTax {
- fn calculate_tax(&self, amount: f32, rate: f32) -> f32;
- }
- trait CompoundTax: SimpleTax {
- fn compound_tax(&self, amount: f32, base_rate: f32, rate: f32) -> f32;
- }
- impl dyn SimpleTax {
- fn calculate_tax(&self, amount: f32, rate: f32) -> f32 {
- amount * rate
- }
- }
- impl dyn CompoundTax
- where
- Self: SimpleTax,
- {
- fn compound_tax(&self, amount: f32, base_rate: f32, compound_rate: f32) -> f32 {
- let base_tax = &self.calculate_tax(amount, base_rate);
- base_tax + amount * compound_rate
- }
- }
- impl SimpleTax for USA {
- fn calculate_tax(&self, amount: f32, rate: f32) -> f32 {
- amount * rate
- }
- }
- impl CompoundTax for USA {
- fn compound_tax(&self, amount: f32, base_rate: f32, compound_rate: f32) -> f32 {
- let base_tax = &self.calculate_tax(amount, base_rate);
- base_tax + amount * compound_rate
- }
- }
- fn get_taxed_amount<C: CompoundTax>(c: C) -> f32 {
- c.compound_tax(200.00, 0.08, 0.015)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement