uint!
macro for Uint
literalsThe above can also be written using the [uint!
] macro. Within the macro arguments,
you can write Uint
literals using the same syntax as Rust integer
literals, but using a capital U
in the suffix instead of lowercase.
To use it simply import it in scope:
rust
use ruint::uint;
Now constants can be created in decimal, hex, binary and even octal:
```rust
let avogadro = uint!(602214076000000000000000U256); let cowkey = uint!(0xee79b5f6e221356af78cf4c36f4f7885a11b67dfcc81c34d80249947330c0f82U256); let bender = uint!(0b1010011010_U10); ```
The [uint!
] macro recurses through the parse tree, so the above can equivalently be written
```rust
uint!{ let avogadro = 602214076000000000000000U256; let cowkey = 0xee79b5f6e221356af78cf4c36f4f7885a11b67dfcc81c34d80249947330c0f82U256; let bender = 0b1010011010_U10; } ```
This latter form is particularly useful for lookup tables:
```rust
const PRIMES: [Uint<128>; 3] = uint!([ 170141183460469231731687303715884105757U128, 170141183460469231731687303715884105773U128, 170141183460469231731687303715884105793_U128, ]); ```
The macro will throw a compile time error if you try to create a constant that does not fit the type:
```rust,compile_fail
let sparta = 300_U8;
```
text,ignore
error: Value too large for Uint<8>: 300
--> src/example.rs:1:14
|
1 | let sparta = 300_U8;
| ^^^^^^