liveBook · Manning

Const generics

Const generics are generic parameters whose values are compile-time constants rather than types or runtime values.

How 2 Manning books explain it

Learn Rust in a Month of Lunches

Const generics are Rust generics that parameterize types or implementations with compile-time constant values.

How it is used
Uses compile-time values to make array-oriented structs and implementations reusable across lengths.
How deep it goes
Defines const parameters and works through array buffers, but stops short of compiler internals or advanced constraints.
Examples
Demonstrates fixed-size byte buffers, reusable Buffers<T, const N> structs, and concrete arrays with different types and lengths.
What you can do after
Build reusable array-backed structs whose element types and lengths are selected at compile time.

Read how Learn Rust in a Month of Lunches explains const generics →

Idiomatic Rust

A const generic is a Rust generic parameter whose value is a compile-time constant rather than a type.

How it is used
Uses compile-time values as generic parameters to make fixed-size structures, such as arrays, work with arbitrary lengths.
How deep it goes
Defines the syntax, permitted parameter types, and generic array conversion, but does not detail compiler internals.
Examples
Demonstrates a generic Buffer, conversion from arrays, and debug output for a four-element buffer.
What you can do after
Build it from scratch by defining a Buffer with a const length parameter and implementing conversion from arrays.

Read how Idiomatic Rust explains const generics →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.