Trait core::ops::Multiply

pub trait Multiply {
    /// Multiply two values of the same type.
    ///
    /// # Arguments
    ///
    /// * `other`: [Self] - The value to multiply with self.
    ///
    /// # Returns
    ///
    /// * [Self] - The result of the two values multiplied.
    ///
    /// # Examples
    ///
    /// ```sway
    /// struct MyStruct {
    ///     val: u64,
    /// }
    ///
    /// impl Multiply for MyStruct {
    ///     fn multiply(self, other: Self) -> Self {
    ///         let val = self.val * other.val;
    ///         Self {
    ///             val
    ///         }
    ///     }
    /// }
    ///
    /// fn foo() {
    ///     let struct1 = MyStruct { val: 3 };
    ///     let struct2 = MyStruct { val: 2 };
    ///     let result_struct = struct1 * struct2;
    ///     assert(result_struct.val == 6);
    /// }
    /// ```
    fn multiply(self, other: Self) -> Self;
}
Expand description

Trait for the multiplication of two values.

Required Methods

Multiply two values of the same type.

Arguments

  • other: [Self] - The value to multiply with self.

Returns

  • [Self] - The result of the two values multiplied.

Examples

struct MyStruct {
    val: u64,
}

impl Multiply for MyStruct {
    fn multiply(self, other: Self) -> Self {
        let val = self.val * other.val;
        Self {
            val
        }
    }
}

fn foo() {
    let struct1 = MyStruct { val: 3 };
    let struct2 = MyStruct { val: 2 };
    let result_struct = struct1 * struct2;
    assert(result_struct.val == 6);
}