about summary refs log tree commit diff
path: root/src/libcore/ops.rs
diff options
context:
space:
mode:
authorMatthew Piziak <matthew.piziak@gmail.com>2016-08-23 12:09:06 -0400
committerMatthew Piziak <matthew.piziak@gmail.com>2016-08-23 12:09:06 -0400
commit2cad78d5eb6d7f185c75a53851adf2cb271f77b3 (patch)
tree2b5125850f89cbaabc0d2699d13e42d12b1848b8 /src/libcore/ops.rs
parent43204fff5d0a656f8a94bfff3129e04bc9d30ad4 (diff)
downloadrust-2cad78d5eb6d7f185c75a53851adf2cb271f77b3.tar.gz
rust-2cad78d5eb6d7f185c75a53851adf2cb271f77b3.zip
replace `Div` example with something more evocative of division
Analogous to PR #35860.

r? @GuillaumeGomez
Diffstat (limited to 'src/libcore/ops.rs')
-rw-r--r--src/libcore/ops.rs61
1 files changed, 52 insertions, 9 deletions
diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs
index 282f281047e..c9124249bf5 100644
--- a/src/libcore/ops.rs
+++ b/src/libcore/ops.rs
@@ -421,25 +421,68 @@ mul_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
 ///
 /// # Examples
 ///
-/// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
-/// calling `div`, and therefore, `main` prints `Dividing!`.
+/// Implementing a `Div`idable rational number struct:
 ///
 /// ```
 /// use std::ops::Div;
 ///
-/// struct Foo;
+/// // The uniqueness of rational numbers in lowest terms is a consequence of
+/// // the fundamental theorem of arithmetic.
+/// #[derive(Eq)]
+/// #[derive(PartialEq, Debug)]
+/// struct Rational {
+///     nominator: usize,
+///     denominator: usize,
+/// }
 ///
-/// impl Div for Foo {
-///     type Output = Foo;
+/// impl Rational {
+///     fn new(nominator: usize, denominator: usize) -> Self {
+///         if denominator == 0 {
+///             panic!("Zero is an invalid denominator!");
+///         }
 ///
-///     fn div(self, _rhs: Foo) -> Foo {
-///         println!("Dividing!");
-///         self
+///         // Reduce to lowest terms by dividing by the greatest common
+///         // divisor.
+///         let gcd = gcd(nominator, denominator);
+///         Rational {
+///             nominator: nominator / gcd,
+///             denominator: denominator / gcd,
+///         }
+///     }
+/// }
+///
+/// impl Div for Rational {
+///     // The division of rational numbers is a closed operation.
+///     type Output = Self;
+///
+///     fn div(self, rhs: Self) -> Self {
+///         if rhs.nominator == 0 {
+///             panic!("Cannot divide by zero-valued `Rational`!");
+///         }
+///
+///         let nominator = self.nominator * rhs.denominator;
+///         let denominator = self.denominator * rhs.nominator;
+///         Rational::new(nominator, denominator)
+///     }
+/// }
+///
+/// // Euclid's two-thousand-year-old algorithm for finding the greatest common
+/// // divisor.
+/// fn gcd(x: usize, y: usize) -> usize {
+///     let mut x = x;
+///     let mut y = y;
+///     while y != 0 {
+///         let t = y;
+///         y = x % y;
+///         x = t;
 ///     }
+///     x
 /// }
 ///
 /// fn main() {
-///     Foo / Foo;
+///     assert_eq!(Rational::new(1, 2), Rational::new(2, 4));
+///     assert_eq!(Rational::new(1, 2) / Rational::new(3, 4),
+///                Rational::new(2, 3));
 /// }
 /// ```
 ///