num-integer

The Integer trait and generic integer functions for Rust, part of the num crate family.

Library
Cargo
v0.1.47
192stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
63/100Good
Development Activity72
Maintenance28
Community72
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
80/100Excellent
Architecture83
Code Quality85
Innovation68
Learning Curve84

num-integer is a foundational Rust crate providing the Integer trait and a set of generic integer functions. It defines integer-specific operations — greatest common divisor, least common multiple, floored and Euclidean division and remainder, parity checks, and integer roots — that work across all of Rust’s integer types and any custom types implementing the trait.

A member of the widely used rust-num family (alongside num-traits, num-bigint, and others), num-integer is a dependency of an enormous slice of the Rust numerics ecosystem. It supports no_std environments and requires only num-traits, making it a lightweight building block for generic numeric code.

What You Get

  • The Integer trait with gcd, lcm, floored/Euclidean division and remainder
  • Free functions like div_rem, div_floor, and mod_floor for generic code
  • Integer square and cube root helpers (roots.rs)
  • Averaging utilities that avoid overflow (average.rs)
  • no_std support for embedded and constrained targets

Common Use Cases

  • Writing generic numeric algorithms over any integer type
  • Computing GCD/LCM for fractions, ratios, and number theory
  • Performing floored or Euclidean division with correct sign handling
  • Building higher-level numeric crates on a shared integer abstraction

Under The Hood

Architecture - The crate is compact: lib.rs defines the Integer trait and its methods (div_floor, mod_floor, gcd, lcm, gcd_lcm, div_rem, parity checks) plus blanket implementations for the primitive integer types via macros; roots.rs implements integer nth-root algorithms; and average.rs provides overflow-safe floor/ceil averaging. The trait builds directly on num-traits’ Num, PartialOrd, Ord, and Eq bounds so it composes with the rest of the num ecosystem.

Tech Stack - Pure Rust (edition 2018, MSRV as low as 1.31), with a single runtime dependency on num-traits. It is #![no_std]-capable by disabling the default std feature, relying only on core.

Code Quality - As a long-lived rust-num crate it is heavily exercised — it sits under a vast dependency tree — and ships with RELEASES notes, CI, and tested implementations across every primitive integer width. Implementations are generated by macros to guarantee consistency across types, and algorithms like integer roots are carefully written to avoid overflow.

API Design - The API is minimal and idiomatic: implement or rely on Integer, then call methods or the mirrored free functions. Naming follows Rust conventions (div_floor/mod_floor), and because it’s generic, downstream crates get integer operations that just work over any conforming type. Its narrow surface makes it easy to learn, though it is a building block rather than an end-user tool.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search