Crate num_bigint
Big Integer Types for Rust
- A
BigUintis unsigned and represented as a vector of digits. - A
BigIntis signed and is a combination ofBigUintandSign.
Common numerical operations are overloaded, so we can treat them the same way we treat other numbers.
Example
#
It's easy to generate large random numbers:
use ;
let mut rng = thread_rng;
let a = rng.gen_bigint;
let low = -10000.to_bigint.unwrap;
let high = 10000.to_bigint.unwrap;
let b = rng.gen_bigint_range;
// Probably an even larger number.
println!;
See the "Features" section for instructions for enabling random number generation.
Features
The std crate feature is enabled by default, which enables std::error::Error
implementations and some internal use of floating point approximations. This can be disabled by
depending on num-bigint with default-features = false. Either way, the alloc crate is
always required for heap allocation of the BigInt/BigUint digits.
Random Generation
num-bigint supports the generation of random big integers when the rand
feature is enabled. To enable it include rand as
rand = "0.8"
num-bigint = { version = "0.4", features = ["rand"] }
Note that you must use the version of rand that num-bigint is compatible
with: 0.8.
Arbitrary Big Integers
num-bigint supports arbitrary and quickcheck features to implement
arbitrary::Arbitrary and quickcheck::Arbitrary, respectively, for both BigInt and
BigUint. These are useful for fuzzing and other forms of randomized testing.
Serialization
The serde feature adds implementations of [Serialize][serde::Serialize] and
[Deserialize][serde::Deserialize] for both BigInt and BigUint. Their serialized data is
generated portably, regardless of platform differences like the internal digit size.
Compatibility
The num-bigint crate is tested for rustc 1.60 and greater.
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
u32digits representation of aBigUintorBigInt, ordered least significant digit first. -
U64Digits
An iterator of
u64digits representation of aBigUintorBigInt, ordered least significant digit first.