Advertisement
cwchen

[Rust] Implementing Add trait for Rational

Sep 4th, 2017
3,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.28 KB | None | 0 0
  1. // Implement binary '+' operation
  2. impl Add for Rational {
  3.     type Output = Rational;
  4.  
  5.     fn add(self, other: Rational) -> Rational {
  6.          let p = self.num * other.denom + other.num * self.denom;
  7.          let q = self.denom * other.denom;
  8.          Rational::new(p, q)
  9.      }
  10.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement