Description
TL;DR
There is a missing chapter/section/paragraph about const generics.
My use-case
Hi there!
I'm a software engineer with 12 years of experience writing code in Python, C++, Java and other programming languages, however I'm very new to Rust. I LOVE the language! I've been playing with it for a few weeks, and I'm fascinated by the way the language was designed, which is very different than other languages I know.
As part of me playing with the language, I tried to create a class that gets a list of player structs with known length in compile-time. My first approach was something like:
use crate::player::Player;
use std::array::from_fn;
const PLAYERS_SIZE: usize = 1000;
pub struct Population {
players: [Player; PLAYERS_SIZE]
}
impl Population {
pub fn new() -> Population {
let players: [Player; PLAYERS_SIZE] = from_fn(|i|Player::random_player());
Population { players }
}
}
However, I later saw in Stack-Overflow that I could use const generics instead, which is very similar to the same concept in C++:
use crate::player::Player;
use std::array::from_fn;
pub struct Population<const N: usize> {
players: [Player; N]
}
impl<const N: usize> Population<N> {
pub fn new() -> Population<N> {
let players: [Player; N] = from_fn(|i|Player::random_player());
Population { players }
}
}
I tried to learn more about it from the Rust Book but couldn't find any information there.
Then I saw in #2776 that somebody already tried to add a chapter about const generics, but it wasn't merged.
My questions are:
- Are you planning on adding const generics to the book?
- Would you like to add it as its own chapter, as another section in the generics chapter, or as a short paragraph there.
Please let me know the answer to these questions and I would gladly write that chapter/section/paragraph for you!