Miscellaneous

Big integers

num-badge cat-science-badge

Add the num_bigint crate to your own project:

cargo add num_bigint

Calculation for integers exceeding 128 bits are possible with BigInt.

use num_bigint::{BigInt, ToBigInt};

fn factorial(x: i32) -> BigInt {
    if let Some(mut factorial) = 1.to_bigint() {
        for i in 1..=x {
            factorial = factorial * i;
        }
        factorial
    }
    else {
        panic!("Failed to calculate factorial!");
    }
}

fn main() {
    println!("{}! equals {}", 100, factorial(100));
}