Trait core::ops::Subtract

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

Trait for the subtraction of two values.

Required Methods

Subtract two values of the same type.

Arguments

  • other: [Self] - The value to subtract from self.

Returns

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

Examples

struct MyStruct {
    val: u64,
}

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

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