Module num_bigint

Source
Expand description

Arbitrary precision integers.


num-bigint provides BigInt and BigUint types for arbitrary precision integer arithmetic. These types can represent integers of any size, limited only by available memory.

BigInt is a signed arbitrary precision integer, while BigUint is an unsigned arbitrary precision integer. Both support the standard arithmetic operations and can be converted to and from standard integer types and strings.

The types implement many of the same traits as the built-in integer types, making them easy to use as drop-in replacements when you need larger integers.

§Examples

Basic arithmetic with big integers:

use num_bigint::{BigInt, BigUint, ToBigInt};

// Create big integers from standard types
let a = BigInt::from(42);
let b = "123456789012345678901234567890".parse::<BigInt>().unwrap();

// Arithmetic operations work as expected
let sum = &a + &b;
let product = &a * &b;

// Convert back to strings for display
println!("Sum: {}", sum);
println!("Product: {}", product);

Computing large factorials:

use num_bigint::BigUint;

fn factorial(n: u32) -> BigUint {
    (1..=n).map(BigUint::from).fold(BigUint::from(1u32), |acc, x| acc * x)
}

// Compute 100! (which is very large)
let result = factorial(100);
println!("100! = {}", result);

Structs§

BigInt
A big signed integer type.
BigUint
A big unsigned integer type.
ParseBigIntError
TryFromBigIntError
The error type returned when a checked conversion regarding big integer fails.
U32Digits
An iterator of u32 digits representation of a BigUint or BigInt, ordered least significant digit first.
U64Digits
An iterator of u64 digits representation of a BigUint or BigInt, ordered least significant digit first.

Enums§

Sign
A Sign is a BigInt’s composing element.

Traits§

ToBigInt
A generic trait for converting a value to a BigInt. This may return None when converting from f32 or f64, and will always succeed when converting from any integer or unsigned primitive, or BigUint.
ToBigUint
A generic trait for converting a value to a BigUint.