Performs truncated division and remainder. T – dividend type (left-hand operand) U – divisor type (right-hand operand, must be wrapped in NonZero at call-site) The division truncates toward zero, like Cairo’s / and %.

Signature

pub trait DivRem

Associated types

  • Quotient – the type produced by the division
  • Remainder – the type produced by the modulo

Examples

Identical operand types:
use core::traits::{DivRem, NonZero};

let lhs: u32 = 7;
let rhs: NonZero = 3.try_into().unwrap();
assert!(DivRem::::div_rem(lhs, rhs) == (2, 1));
Heterogeneous division (u256 by u128):
use core::traits::DivRem;
use integer::u256_as_non_zero;

let big: u256 = 1_000_000;                    // dividend
let nz10: NonZero = u256_as_non_zero(10_u128.into());  // divisor

let (q, r) = DivRem::::div_rem(big, nz10);
// q : u256, r : u128

Trait functions

div_rem

Computes both / and % in a single pass.

Signature

fn div_rem(self: T, other: NonZero) -> (DivRemQuotient, DivRemRemainder)

Trait types

Quotient

Quotient returned by the division.

Signature

type Quotient;

Remainder

Remainder returned by the modulo operation.

Signature

type Remainder;