Rs
Module 6Traits & GenericsLesson 4 of 822 min

Traits: Payoff Interface

Call/Put behind a trait.

Trait = interface

rust
trait Payoff {
    fn payoff(&self, spot: f64) -> f64;
}

Call: max(S-K,0); Put: max(K-S,0).

MC engines can price anything implementing Payoff.

Challenge

Add DigitalCall { strike, cash } implementing Payoff.

Tips
  • max on f64
  • Traits = polymorphism
  • Default methods exist

Try it live

Traits: Payoff InterfaceRust 2021
Loading editor…
Output
Press Run to compile and execute.