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 uint;
Now constants can be created in decimal, hex, binary and even octal:
# use uint;
let avogadro = uint!;
let cow_key = uint!;
let bender = uint!;
The [uint!
] macro recurses through the parse tree, so the above can equivalently be written
# use uint;
uint!
This latter form is particularly useful for lookup tables:
# use ;
const PRIMES: = uint!;
The macro will throw a compile time error if you try to create a constant that does not fit the type:
# use uint;
# uint!
error: Value too large for Uint<8>: 300
--> src/example.rs:1:14
|
1 | let sparta = 300_U8;
| ^^^^^^
References
- Rust integer literals syntax.