News for hackers. Stuff that matters.
Re:Grids in Rust, Part 2: const generics by jimktrains2 on Saturday April 03, 2021 @02:45PM (#26683581)
The size of all stack-allocated items needs to be known at compile-time. It's the same reason you can't directly have a field in a struct that is the same type as the struct: the size can't be known at compile-time.

If you need an array of unknown size, then you can heap allocate it or use std::vec::Vec (usually via the vec! macro) to provide a nice interface for doing so.

If you need nested structs, e.g. for a tree, then you need to make the field, e.g. the children nodes, pointers or a Box type.

Rust tries to make what it's doing with memory obvious. Other languages with unknown array sizes do a lot of potentially unpredictable memory allocations and copies.

There are no comments on this comment yet.