9 releases (5 stable)

1.2.0 Feb 27, 2024
1.1.0 Jul 25, 2023
1.0.2 Nov 1, 2022
1.0.1 Oct 24, 2022
0.1.1 May 15, 2022

#1257 in Math

Download history 14723/week @ 2023-12-15 15705/week @ 2023-12-22 18323/week @ 2023-12-29 30667/week @ 2024-01-05 35569/week @ 2024-01-12 40262/week @ 2024-01-19 41402/week @ 2024-01-26 38370/week @ 2024-02-02 38910/week @ 2024-02-09 47798/week @ 2024-02-16 46513/week @ 2024-02-23 43242/week @ 2024-03-01 47478/week @ 2024-03-08 47707/week @ 2024-03-15 47687/week @ 2024-03-22 44335/week @ 2024-03-29

194,733 downloads per month
Used in 115 crates (2 directly)

MIT license

14KB
235 lines

The uint! macro for Uint and Bits literals

Within the uint! macro arguments, you can write Uint and Bits literals using the same syntax as Rust integer literals, but using a capital U or B suffix respectively. Note that there is ambiguity for hexadecimals with a B suffix, to lessen the impact an underscore is required in this case.

To use it simply import it in scope:

use ruint::uint;

Now constants can be created in decimal, hex, binary and even octal:

# use ruint::uint;
let avogadro = uint!(602_214_076_000_000_000_000_000_U256);
let cow_key = uint!(0xee79b5f6e221356af78cf4c36f4f7885a11b67dfcc81c34d80249947330c0f82_U256);
let bender = uint!(0b1010011010_U10);

The uint! macro recurses through the parse tree, so the above can equivalently be written

# use ruint::uint;
uint! {
let avogadro = 602_214_076_000_000_000_000_000_U256;
let cow_key = 0xee79b5f6e221356af78cf4c36f4f7885a11b67dfcc81c34d80249947330c0f82_U256;
let bender = 0b1010011010_U10;
}

This latter form is particularly useful for lookup tables:

# use ruint::{Uint, uint};
const PRIMES: [Uint<128, 2>; 3] = uint!([
    170141183460469231731687303715884105757_U128,
    170141183460469231731687303715884105773_U128,
    170141183460469231731687303715884105793_U128,
]);

The macro will throw a compile time error if you try to create a constant that does not fit the type:

# use ruint::uint;
# uint! {
let sparta = 300_U8;
# }
error: Value too large for Uint<8>: 300
 --> src/example.rs:1:14
  |
1 | let sparta = 300_U8;
  |              ^^^^^^

References

No runtime deps