Trait core::ops::Mod

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

Trait for the modulo of two values.

Required Methods

Modulo two values of the same type.

Arguments

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

Returns

  • [Self] - The modulo of the two values.

Examples

struct MyStruct {
    val: u64,
}

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

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result_struct = struct1 % struct2;
    assert(result_struct.val == 0);
}