pub trait Divide {
/// Divide two values of the same type.
///
/// # Arguments
///
/// * `other`: [Self] - The value to divide with self.
///
/// # Returns
///
/// * [Self] - The result of the two values divided.
///
/// # Examples
///
/// ```sway
/// struct MyStruct {
/// val: u64,
/// }
///
/// impl Divide for MyStruct {
/// fn divide(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 == 5);
/// }
/// ```
fn divide(self, other: Self) -> Self;
}
Expand description
Trait for the division of two values.
Required Methods
fn divide(self, other: Self) -> Self
fn divide(self, other: Self) -> Self
Divide two values of the same type.
Arguments
other
: [Self] - The value to divide with self.
Returns
- [Self] - The result of the two values divided.
Examples
struct MyStruct {
val: u64,
}
impl Divide for MyStruct {
fn divide(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 == 5);
}