News for hackers. Stuff that matters.
Re:Grids in Rust, Part 2: const generics by NobodyNada on Saturday April 03, 2021 @02:44PM (#26683577)
> This restriction is not found in other programming languages

It is. C used to have variable-length arrays as a required part of the standard, but that was made an optional feature in C99. C++ (like Rust) does not have variable-length arrays at all.

If you need a dynamically-sized array in any language, usually you allocate the array on the heap — for instance, with malloc/free, or with a vector type like C++’s std::vector or Rust’s Vec.

As for why, I’m not entirely sure — hopefully someone with more expertise than me can chime in. I’m not sure if it adds too much complexity in the compiler, or if they were simply deemed too unsafe even for C/C++ (since it’s way too easy to accidentally overflow the stack with VLAs).

Re:Grids in Rust, Part 2: const generics by jessa0 on Saturday April 03, 2021 @04:27PM (#26684427)
Slices in Rust are actually kinda similar to VLAs in C99, but usable in places other than aggregates, and use an additional word in pointers to them for the length, as a safety mechanism. You can even use slices as the last member of a struct, like VLAs, though they're not very ergonomic currently (you can only obtain references to them, as well as Boxes containing them, using "unsizing" coercion):

https://play.rust-lang.org/?version=stable&mode=debug&editio...

Re:Grids in Rust, Part 2: const generics by jessa0 on Saturday April 03, 2021 @10:12PM (#26686383)
Ok, turns out I was mixing up "flexible array member" with "variable-length array". Slices can be used on the stack like a VLA as well, though, with the unstable "unsized_locals" feature. This feature is controversial, however, because of the risk of unintentional stack blowup..
Re:Grids in Rust, Part 2: const generics by oconnor663 on Saturday April 03, 2021 @06:32PM (#26685335)
The Linux kernel devs have banned VLAs from their codebase, so you can get a lot of context from that discussion. (https://www.phoronix.com/scan.php?page=news_item&px=Linux-Ki...). One of the drawbacks of VLAs is that, maybe just because of an unfortunate choice of syntax, it's surprisingly easy to create one accidentally: https://lwn.net/Articles/749064/
Re:Grids in Rust, Part 2: const generics by kzrdude on Saturday April 03, 2021 @02:53PM (#26683652)
Vararrays were new (and required) in C99 and optional in C11