pub trait Mul<Rhs = Self> {
type Output;
fn mul(self, rhs: Rhs) -> Self::Output;
}
Expand description
The multiplication operator *
.
Note that Rhs
is Self
by default, but this is not mandatory.
use std::ops::Mul;
#[derive(Debug, Eq, PartialEq)]
struct Rational {
numerator: usize,
denominator: usize,
}
impl Rational {
fn new(numerator: usize, denominator: usize) -> Self {
if denominator == 0 {
panic!("Zero is an invalid denominator!");
}
let gcd = gcd(numerator, denominator);
Self {
numerator: numerator / gcd,
denominator: denominator / gcd,
}
}
}
impl Mul for Rational {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
let numerator = self.numerator * rhs.numerator;
let denominator = self.denominator * rhs.denominator;
Self::new(numerator, denominator)
}
}
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));
use std::ops::Mul;
struct Scalar { value: usize }
#[derive(Debug, PartialEq)]
struct Vector { value: Vec<usize> }
impl Mul<Scalar> for Vector {
type Output = Self;
fn mul(self, rhs: Scalar) -> Self::Output {
Self { value: self.value.iter().map(|v| v * rhs.value).collect() }
}
}
let vector = Vector { value: vec![2, 4, 6] };
let scalar = Scalar { value: 3 };
assert_eq!(vector * scalar, Vector { value: vec![6, 12, 18] });
The resulting type after applying the *
operator.
Performs the *
operation.
impl Mul<LittleEndian<f32>> for LittleEndian<f32>
impl Mul<LittleEndian<f64>> for LittleEndian<f64>
impl Mul<LittleEndian<i16>> for LittleEndian<i16>
impl Mul<LittleEndian<i32>> for LittleEndian<i32>
impl Mul<LittleEndian<i64>> for LittleEndian<i64>
impl Mul<LittleEndian<u16>> for LittleEndian<u16>
impl Mul<LittleEndian<u32>> for LittleEndian<u32>
impl Mul<LittleEndian<u64>> for LittleEndian<u64>
impl Mul<NativeEndian<f32>> for NativeEndian<f32>
impl Mul<NativeEndian<f64>> for NativeEndian<f64>
impl Mul<NativeEndian<i16>> for NativeEndian<i16>
impl Mul<NativeEndian<i32>> for NativeEndian<i32>
impl Mul<NativeEndian<i64>> for NativeEndian<i64>
impl Mul<NativeEndian<u16>> for NativeEndian<u16>
impl Mul<NativeEndian<u32>> for NativeEndian<u32>
impl Mul<NativeEndian<u64>> for NativeEndian<u64>
impl<'_> Mul<&'_ BigEndian<f32>> for BigEndian<f32>
impl<'_> Mul<&'_ BigEndian<f64>> for BigEndian<f64>
impl<'_> Mul<&'_ BigEndian<i16>> for BigEndian<i16>
impl<'_> Mul<&'_ BigEndian<i32>> for BigEndian<i32>
impl<'_> Mul<&'_ BigEndian<i64>> for BigEndian<i64>
impl<'_> Mul<&'_ BigEndian<u16>> for BigEndian<u16>
impl<'_> Mul<&'_ BigEndian<u32>> for BigEndian<u32>
impl<'_> Mul<&'_ BigEndian<u64>> for BigEndian<u64>
impl<'_> Mul<&'_ LittleEndian<f32>> for LittleEndian<f32>
impl<'_> Mul<&'_ LittleEndian<f64>> for LittleEndian<f64>
impl<'_> Mul<&'_ LittleEndian<i16>> for LittleEndian<i16>
impl<'_> Mul<&'_ LittleEndian<i32>> for LittleEndian<i32>
impl<'_> Mul<&'_ LittleEndian<i64>> for LittleEndian<i64>
impl<'_> Mul<&'_ LittleEndian<i128>> for LittleEndian<i128>
impl<'_> Mul<&'_ LittleEndian<u16>> for LittleEndian<u16>
impl<'_> Mul<&'_ LittleEndian<u32>> for LittleEndian<u32>
impl<'_> Mul<&'_ LittleEndian<u64>> for LittleEndian<u64>
impl<'_> Mul<&'_ LittleEndian<u128>> for LittleEndian<u128>
impl<'_> Mul<&'_ NativeEndian<f32>> for NativeEndian<f32>
impl<'_> Mul<&'_ NativeEndian<f64>> for NativeEndian<f64>
impl<'_> Mul<&'_ NativeEndian<i16>> for NativeEndian<i16>
impl<'_> Mul<&'_ NativeEndian<i32>> for NativeEndian<i32>
impl<'_> Mul<&'_ NativeEndian<i64>> for NativeEndian<i64>
impl<'_> Mul<&'_ NativeEndian<i128>> for NativeEndian<i128>
impl<'_> Mul<&'_ NativeEndian<u16>> for NativeEndian<u16>
impl<'_> Mul<&'_ NativeEndian<u32>> for NativeEndian<u32>
impl<'_> Mul<&'_ NativeEndian<u64>> for NativeEndian<u64>
impl<'_> Mul<&'_ NativeEndian<u128>> for NativeEndian<u128>
impl<'_> Mul<BigEndian<f32>> for &'_ BigEndian<f32>
impl<'_> Mul<BigEndian<f64>> for &'_ BigEndian<f64>
impl<'_> Mul<BigEndian<i16>> for &'_ BigEndian<i16>
impl<'_> Mul<BigEndian<i32>> for &'_ BigEndian<i32>
impl<'_> Mul<BigEndian<i64>> for &'_ BigEndian<i64>
impl<'_> Mul<BigEndian<u16>> for &'_ BigEndian<u16>
impl<'_> Mul<BigEndian<u32>> for &'_ BigEndian<u32>
impl<'_> Mul<BigEndian<u64>> for &'_ BigEndian<u64>
impl<'_> Mul<LittleEndian<f32>> for &'_ LittleEndian<f32>
impl<'_> Mul<LittleEndian<f64>> for &'_ LittleEndian<f64>
impl<'_> Mul<LittleEndian<i16>> for &'_ LittleEndian<i16>
impl<'_> Mul<LittleEndian<i32>> for &'_ LittleEndian<i32>
impl<'_> Mul<LittleEndian<i64>> for &'_ LittleEndian<i64>
impl<'_> Mul<LittleEndian<i128>> for &'_ LittleEndian<i128>
impl<'_> Mul<LittleEndian<u16>> for &'_ LittleEndian<u16>
impl<'_> Mul<LittleEndian<u32>> for &'_ LittleEndian<u32>
impl<'_> Mul<LittleEndian<u64>> for &'_ LittleEndian<u64>
impl<'_> Mul<LittleEndian<u128>> for &'_ LittleEndian<u128>
impl<'_> Mul<NativeEndian<f32>> for &'_ NativeEndian<f32>
impl<'_> Mul<NativeEndian<f64>> for &'_ NativeEndian<f64>
impl<'_> Mul<NativeEndian<i16>> for &'_ NativeEndian<i16>
impl<'_> Mul<NativeEndian<i32>> for &'_ NativeEndian<i32>
impl<'_> Mul<NativeEndian<i64>> for &'_ NativeEndian<i64>
impl<'_> Mul<NativeEndian<i128>> for &'_ NativeEndian<i128>
impl<'_> Mul<NativeEndian<u16>> for &'_ NativeEndian<u16>
impl<'_> Mul<NativeEndian<u32>> for &'_ NativeEndian<u32>
impl<'_> Mul<NativeEndian<u64>> for &'_ NativeEndian<u64>
impl<'_> Mul<NativeEndian<u128>> for &'_ NativeEndian<u128>
impl<'_, '_> Mul<&'_ f32> for &'_ BigEndian<f32>
impl<'_, '_> Mul<&'_ f32> for &'_ LittleEndian<f32>
impl<'_, '_> Mul<&'_ f32> for &'_ NativeEndian<f32>
impl<'_, '_> Mul<&'_ f64> for &'_ BigEndian<f64>
impl<'_, '_> Mul<&'_ f64> for &'_ LittleEndian<f64>
impl<'_, '_> Mul<&'_ f64> for &'_ NativeEndian<f64>
impl<'_, '_> Mul<&'_ i16> for &'_ BigEndian<i16>
impl<'_, '_> Mul<&'_ i16> for &'_ LittleEndian<i16>
impl<'_, '_> Mul<&'_ i16> for &'_ NativeEndian<i16>
impl<'_, '_> Mul<&'_ i32> for &'_ BigEndian<i32>
impl<'_, '_> Mul<&'_ i32> for &'_ LittleEndian<i32>
impl<'_, '_> Mul<&'_ i32> for &'_ NativeEndian<i32>
impl<'_, '_> Mul<&'_ i64> for &'_ BigEndian<i64>
impl<'_, '_> Mul<&'_ i64> for &'_ LittleEndian<i64>
impl<'_, '_> Mul<&'_ i64> for &'_ NativeEndian<i64>
impl<'_, '_> Mul<&'_ u16> for &'_ BigEndian<u16>
impl<'_, '_> Mul<&'_ u16> for &'_ LittleEndian<u16>
impl<'_, '_> Mul<&'_ u16> for &'_ NativeEndian<u16>
impl<'_, '_> Mul<&'_ u32> for &'_ BigEndian<u32>
impl<'_, '_> Mul<&'_ u32> for &'_ LittleEndian<u32>
impl<'_, '_> Mul<&'_ u32> for &'_ NativeEndian<u32>
impl<'_, '_> Mul<&'_ u64> for &'_ BigEndian<u64>
impl<'_, '_> Mul<&'_ u64> for &'_ LittleEndian<u64>
impl<'_, '_> Mul<&'_ u64> for &'_ NativeEndian<u64>
impl<'_, '_> Mul<&'_ BigEndian<f32>> for &'_ f32
impl<'_, '_> Mul<&'_ BigEndian<f32>> for &'_ BigEndian<f32>
impl<'_, '_> Mul<&'_ BigEndian<f64>> for &'_ f64
impl<'_, '_> Mul<&'_ BigEndian<f64>> for &'_ BigEndian<f64>
impl<'_, '_> Mul<&'_ BigEndian<i16>> for &'_ i16
impl<'_, '_> Mul<&'_ BigEndian<i16>> for &'_ BigEndian<i16>
impl<'_, '_> Mul<&'_ BigEndian<i32>> for &'_ i32
impl<'_, '_> Mul<&'_ BigEndian<i32>> for &'_ BigEndian<i32>
impl<'_, '_> Mul<&'_ BigEndian<i64>> for &'_ i64
impl<'_, '_> Mul<&'_ BigEndian<i64>> for &'_ BigEndian<i64>
impl<'_, '_> Mul<&'_ BigEndian<i128>> for &'_ BigEndian<i128>
impl<'_, '_> Mul<&'_ BigEndian<u16>> for &'_ u16
impl<'_, '_> Mul<&'_ BigEndian<u16>> for &'_ BigEndian<u16>
impl<'_, '_> Mul<&'_ BigEndian<u32>> for &'_ u32
impl<'_, '_> Mul<&'_ BigEndian<u32>> for &'_ BigEndian<u32>
impl<'_, '_> Mul<&'_ BigEndian<u64>> for &'_ u64
impl<'_, '_> Mul<&'_ BigEndian<u64>> for &'_ BigEndian<u64>
impl<'_, '_> Mul<&'_ BigEndian<u128>> for &'_ BigEndian<u128>
impl<'_, '_> Mul<&'_ LittleEndian<f32>> for &'_ f32
impl<'_, '_> Mul<&'_ LittleEndian<f32>> for &'_ LittleEndian<f32>
impl<'_, '_> Mul<&'_ LittleEndian<f64>> for &'_ f64
impl<'_, '_> Mul<&'_ LittleEndian<f64>> for &'_ LittleEndian<f64>
impl<'_, '_> Mul<&'_ LittleEndian<i16>> for &'_ i16
impl<'_, '_> Mul<&'_ LittleEndian<i16>> for &'_ LittleEndian<i16>
impl<'_, '_> Mul<&'_ LittleEndian<i32>> for &'_ i32
impl<'_, '_> Mul<&'_ LittleEndian<i32>> for &'_ LittleEndian<i32>
impl<'_, '_> Mul<&'_ LittleEndian<i64>> for &'_ i64
impl<'_, '_> Mul<&'_ LittleEndian<i64>> for &'_ LittleEndian<i64>
impl<'_, '_> Mul<&'_ LittleEndian<i128>> for &'_ LittleEndian<i128>
impl<'_, '_> Mul<&'_ LittleEndian<u16>> for &'_ u16
impl<'_, '_> Mul<&'_ LittleEndian<u16>> for &'_ LittleEndian<u16>
impl<'_, '_> Mul<&'_ LittleEndian<u32>> for &'_ u32
impl<'_, '_> Mul<&'_ LittleEndian<u32>> for &'_ LittleEndian<u32>
impl<'_, '_> Mul<&'_ LittleEndian<u64>> for &'_ u64
impl<'_, '_> Mul<&'_ LittleEndian<u64>> for &'_ LittleEndian<u64>
impl<'_, '_> Mul<&'_ LittleEndian<u128>> for &'_ LittleEndian<u128>
impl<'_, '_> Mul<&'_ NativeEndian<f32>> for &'_ f32
impl<'_, '_> Mul<&'_ NativeEndian<f32>> for &'_ NativeEndian<f32>
impl<'_, '_> Mul<&'_ NativeEndian<f64>> for &'_ f64
impl<'_, '_> Mul<&'_ NativeEndian<f64>> for &'_ NativeEndian<f64>
impl<'_, '_> Mul<&'_ NativeEndian<i16>> for &'_ i16
impl<'_, '_> Mul<&'_ NativeEndian<i16>> for &'_ NativeEndian<i16>
impl<'_, '_> Mul<&'_ NativeEndian<i32>> for &'_ i32
impl<'_, '_> Mul<&'_ NativeEndian<i32>> for &'_ NativeEndian<i32>
impl<'_, '_> Mul<&'_ NativeEndian<i64>> for &'_ i64
impl<'_, '_> Mul<&'_ NativeEndian<i64>> for &'_ NativeEndian<i64>
impl<'_, '_> Mul<&'_ NativeEndian<i128>> for &'_ NativeEndian<i128>
impl<'_, '_> Mul<&'_ NativeEndian<u16>> for &'_ u16
impl<'_, '_> Mul<&'_ NativeEndian<u16>> for &'_ NativeEndian<u16>
impl<'_, '_> Mul<&'_ NativeEndian<u32>> for &'_ u32
impl<'_, '_> Mul<&'_ NativeEndian<u32>> for &'_ NativeEndian<u32>
impl<'_, '_> Mul<&'_ NativeEndian<u64>> for &'_ u64
impl<'_, '_> Mul<&'_ NativeEndian<u64>> for &'_ NativeEndian<u64>
impl<'_, '_> Mul<&'_ NativeEndian<u128>> for &'_ NativeEndian<u128>