Const generic (chess)

My little chessprogram is outperforming the famous stockfish engine with its perft test.
And this after two weeks of rust programming :slight_smile:
The 'monomorphization' (as I believe it is called) works great. Now...
Question 1: I cannot find a clear explanation how to constraint const generic values.
(a "where MODE < 2," does not compile.)
Question 2: It would be even better if I could stuff a const struct or trait into this const parameters so that it would be really typed (and automatically constrained).

fn generate<const MODE: u8, const GENTYPE: u8, const US: u8, const THEM: u8>(&mut self, pos: &Position)

Both of your wishes requires nightly. #![feature(generic_const_exprs)] to constrain generic parameters (Rust Playground), and #![feature(adt_const_params)] for the second (Rust Playground).

Alternatively, you can constrain generic parameters with post-monomorphization errors (Rust Playground). This won't show up in cargo check, only cargo build.

1 Like


This is nice, but note that move generation is usually not the bottleneck in chess programs, so I don't know how much Stockfish's move generation was optimized.

Stockfish (C++) is optimized to the bone. Lots of templates.
I copied the playground code and will check on that. These 'nightly builds' are features which may or may not be in a next version?

Yes nightly features are unstable and may never be part of stable rust. Most are highly experimental.

Optimized on things that matter for engine performance. I didn't say they didn't optimize move generation, I said they might not have optimized it to the maximum extent possible.

Yes. In particular, you must be running nightly to use them, and there are no stability guarantee around them (they may be removed or changed before stabilized, if ever), In particular, generic_const_exprs is very likely to not be stabilized soon, nor in the current form (which is why the compiler warns when you use it).

Thanks! Clear