daily pastebin goal
50%
SHARE
TWEET

[Rust] Implementing Display trait for Rational

cwchen Sep 4th, 2017 301 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. impl fmt::Display for Rational {
  2.     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  3.         let is_positive = (self.num > 0 && self.denom > 0)
  4.             || (self.num < 0 && self.denom < 0);
  5.  
  6.         let sign = if is_positive { "" } else { "-" };
  7.  
  8.         write!(f, "{}{}/{}", sign, self.num.abs(), self.denom.abs())
  9.     }
  10. }
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