@@ -299,26 +299,63 @@ sub_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
299299///
300300/// # Examples
301301///
302- /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
303- /// calling `mul`, and therefore, `main` prints `Multiplying!`.
302+ /// Implementing a `Mul`tipliable rational number struct:
304303///
305304/// ```
306305/// use std::ops::Mul;
307306///
308- /// struct Foo;
307+ /// // The uniqueness of rational numbers in lowest terms is a consequence of
308+ /// // the fundamental theorem of arithmetic.
309+ /// #[derive(Eq)]
310+ /// #[derive(PartialEq, Debug)]
311+ /// struct Rational {
312+ /// nominator: usize,
313+ /// denominator: usize,
314+ /// }
309315///
310- /// impl Mul for Foo {
311- /// type Output = Foo;
316+ /// impl Rational {
317+ /// fn new(nominator: usize, denominator: usize) -> Self {
318+ /// if denominator == 0 {
319+ /// panic!("Zero is an invalid denominator!");
320+ /// }
312321///
313- /// fn mul(self, _rhs: Foo) -> Foo {
314- /// println!("Multiplying!");
315- /// self
322+ /// // Reduce to lowest terms by dividing by the greatest common
323+ /// // divisor.
324+ /// let gcd = gcd(nominator, denominator);
325+ /// Rational {
326+ /// nominator: nominator / gcd,
327+ /// denominator: denominator / gcd,
328+ /// }
316329/// }
317330/// }
318331///
319- /// fn main() {
320- /// Foo * Foo;
332+ /// impl Mul for Rational {
333+ /// // The multiplication of rational numbers is a closed operation.
334+ /// type Output = Self;
335+ ///
336+ /// fn mul(self, rhs: Self) -> Self {
337+ /// let nominator = self.nominator * rhs.nominator;
338+ /// let denominator = self.denominator * rhs.denominator;
339+ /// Rational::new(nominator, denominator)
340+ /// }
321341/// }
342+ ///
343+ /// // Euclid's two-thousand-year-old algorithm for finding the greatest common
344+ /// // divisor.
345+ /// fn gcd(x: usize, y: usize) -> usize {
346+ /// let mut x = x;
347+ /// let mut y = y;
348+ /// while y != 0 {
349+ /// let t = y;
350+ /// y = x % y;
351+ /// x = t;
352+ /// }
353+ /// x
354+ /// }
355+ ///
356+ /// assert_eq!(Rational::new(1, 2), Rational::new(2, 4));
357+ /// assert_eq!(Rational::new(2, 3) * Rational::new(3, 4),
358+ /// Rational::new(1, 2));
322359/// ```
323360///
324361/// Note that `RHS = Self` by default, but this is not mandatory. Here is an
0 commit comments