replace Mul
example with something more evocative of multiplication
I may have gone a bit overboard on this one. Numbers are fun. tone down the error message
This commit is contained in:
parent
11f8805887
commit
38f0bca865
1 changed files with 47 additions and 10 deletions
|
@ -274,26 +274,63 @@ sub_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
|
/// Implementing a `Mul`tipliable rational number struct:
|
||||||
/// calling `mul`, and therefore, `main` prints `Multiplying!`.
|
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::ops::Mul;
|
/// use std::ops::Mul;
|
||||||
///
|
///
|
||||||
/// 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 Mul for Foo {
|
/// impl Rational {
|
||||||
/// type Output = Foo;
|
/// fn new(nominator: usize, denominator: usize) -> Self {
|
||||||
|
/// if denominator == 0 {
|
||||||
|
/// panic!("Zero is an invalid denominator!");
|
||||||
|
/// }
|
||||||
///
|
///
|
||||||
/// fn mul(self, _rhs: Foo) -> Foo {
|
/// // Reduce to lowest terms by dividing by the greatest common
|
||||||
/// println!("Multiplying!");
|
/// // divisor.
|
||||||
/// self
|
/// let gcd = gcd(nominator, denominator);
|
||||||
|
/// Rational {
|
||||||
|
/// nominator: nominator / gcd,
|
||||||
|
/// denominator: denominator / gcd,
|
||||||
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// impl Mul for Rational {
|
||||||
/// Foo * Foo;
|
/// // The multiplication of rational numbers is a closed operation.
|
||||||
|
/// type Output = Self;
|
||||||
|
///
|
||||||
|
/// fn mul(self, rhs: Self) -> Self {
|
||||||
|
/// let nominator = self.nominator * rhs.nominator;
|
||||||
|
/// let denominator = self.denominator * rhs.denominator;
|
||||||
|
/// 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
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// assert_eq!(Rational::new(1, 2), Rational::new(2, 4));
|
||||||
|
/// assert_eq!(Rational::new(2, 3) * Rational::new(3, 4),
|
||||||
|
/// Rational::new(1, 2));
|
||||||
/// ```
|
/// ```
|
||||||
#[lang = "mul"]
|
#[lang = "mul"]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue