Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
:timing
:sccache 1Tips and Traps¶
The modulus operator (
%) in Rust is very picky about data types. It is only defined for two integers with the same type.The modulus operation involves division is much slower compared to multiplication. You should avoid using modulus unless you absolutely need it. If you use modulus as simple hashing function, please refer to Fast Hashing Algorithms for faster alternaitves.
8 % 53let x: u64 = 10;
let y: u32 = 6;
x % yx % y
^ expected `u64`, found `u32`
mismatched typesx % y
^ no implementation for `u64 % u32`
cannot mod `u64` by `u32`
help: the trait `Rem<u32>` is not implemented for `u64`let x: u64 = 10;
let y: u32 = 6;
x % (y as u64)4