@@ -295,6 +295,37 @@ sub_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
295295/// Foo * Foo;
296296/// }
297297/// ```
298+ ///
299+ /// Note that `RHS = Self` by default, but this is not mandatory. Here is an
300+ /// implementation which enables multiplication of vectors by scalars, as is
301+ /// done in linear algebra.
302+ ///
303+ /// ```
304+ /// use std::ops::Mul;
305+ ///
306+ /// struct Scalar {value: usize};
307+ ///
308+ /// #[derive(Debug)]
309+ /// struct Vector {value: Vec<usize>};
310+ ///
311+ /// impl Mul<Vector> for Scalar {
312+ /// type Output = Vector;
313+ ///
314+ /// fn mul(self, rhs: Vector) -> Vector {
315+ /// Vector {value: rhs.value.iter().map(|v| self.value * v).collect()}
316+ /// }
317+ /// }
318+ ///
319+ /// impl PartialEq<Vector> for Vector {
320+ /// fn eq(&self, other: &Self) -> bool {
321+ /// self.value == other.value
322+ /// }
323+ /// }
324+ ///
325+ /// let scalar = Scalar{value: 3};
326+ /// let vector = Vector{value: vec![2, 4, 6]};
327+ /// assert_eq!(scalar * vector, Vector{value: vec![6, 12, 18]});
328+ /// ```
298329#[ lang = "mul" ]
299330#[ stable( feature = "rust1" , since = "1.0.0" ) ]
300331pub trait Mul < RHS =Self > {
@@ -349,6 +380,37 @@ mul_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
349380/// Foo / Foo;
350381/// }
351382/// ```
383+ ///
384+ /// Note that `RHS = Self` by default, but this is not mandatory. Here is an
385+ /// implementation which enables division of vectors by scalars, as is done in
386+ /// linear algebra.
387+ ///
388+ /// ```
389+ /// use std::ops::Div;
390+ ///
391+ /// struct Scalar {value: f32};
392+ ///
393+ /// #[derive(Debug)]
394+ /// struct Vector {value: Vec<f32>};
395+ ///
396+ /// impl Div<Scalar> for Vector {
397+ /// type Output = Vector;
398+ ///
399+ /// fn div(self, rhs: Scalar) -> Vector {
400+ /// Vector {value: self.value.iter().map(|v| v / rhs.value).collect()}
401+ /// }
402+ /// }
403+ ///
404+ /// impl PartialEq<Vector> for Vector {
405+ /// fn eq(&self, other: &Self) -> bool {
406+ /// self.value == other.value
407+ /// }
408+ /// }
409+ ///
410+ /// let scalar = Scalar{value: 2f32};
411+ /// let vector = Vector{value: vec![2f32, 4f32, 6f32]};
412+ /// assert_eq!(vector / scalar, Vector{value: vec![1f32, 2f32, 3f32]});
413+ /// ```
352414#[ lang = "div" ]
353415#[ stable( feature = "rust1" , since = "1.0.0" ) ]
354416pub trait Div < RHS =Self > {
0 commit comments