Presentation on Const Generics as of 1.50.0 stable and 1.52.0-nightly
Challenges
- Beware no, or misleading, documentation
- Nothing in Rust Book.
- RFC https://rust-lang.github.io/rfcs/2000-const-generics.html and various issues rust-lang/rust#68436 have tips or examples, but many are invalid!
- Syntax may seem arbitrary:
- Need
::<turbofish at some places, ordinary<elsewhere. - Need
{...}curly brackets around some const generic parameter values, not around others. - Some error messages feel like going in circles.
- Need
Learning
- If your constant generic parameters are not integer/char/boolean, don't downgrade your domain.
- Use
#![feature(const_generics)]to allowstructs andenums asconstgeneric parameters. - Such
struct/enums need#[derive(PartialEq, Eq)](or their implementation). - Trust
nightly.
- Use
- Even though the purpose of (
const) generics is to reuse code, the actual const generics code is repetitious. You'll have to repeatwhereclause in anyimplthat involves a type withconstgeneric parameter, if theconstgeneric parameter affects size of any field(s).