5 ms·
Fortunately under normal circumstances it's almost always better to use a Vec anyway, and this is encouraged by the Book. Having done projects in Rust for the p
by brundolf 6y ago
Fortunately under normal circumstances it's almost always better to use a Vec anyway, and this is encouraged by the Book. Having done projects in Rust for the past ~2 years I didn't even know this limitation existed, because I hardly ever use plain arrays.
- steveklabnik 6y agoIMHO, it's a chicken and egg kind of thing; it's way easier to use Vecs because arrays really need const generics to actually be useful. I am interested to see how this balance changes. I know for my no_std work, const generics for arrays can't come fast enough.
- GolDDranks 6y agoI have also felt the need multiple times. Especially when passing a number of things to a function, and you need to pass ownership. You can't do that with a slice, because that's borrowed, so a Vec used to be the only option. Now there's a one option more that's going to be more lightweight than allocating a Vec :)
- oconnor663 6y agoI've often used the `arrayvec` crate for things like this. Actually one of the things I'm most excited about, once const generics is ready, is seeing something like `arrayvec` in the standard library.
- simias 6y agoArray sizes are static and can be used for optimization, that's a very significant advantage in some situations. As an example, if I'm emulating some RAM module of a console, I'd much rather use a cheap [u8; RAM_LEN] than a Vec that would incur all kinds of runtime checks for nothing. Unfortunately there are many limitations with arrays in Rust at the moment, which make it more annoying than it should be. Generics over the array size is one of them, although unfortunately there are others such as https://github.com/rust-lang/rust/issues/53827 https://github.com/rust-lang/rust/issues/53827
- yodelshady 6y agoNo it isn't! I'm sick of dealing with Vec's that do need to be a defined length to be valid, but could be any length! That's what a type system is for! But nobody uses the proper type for that, because actually working with the proper type is so awful! It's the single worst aspect of working with Rust. I am very happy.
- ComputerGuru 6y agoConst generics will fix all this; they can’t come a moment too soon, in my opinion.
- woah 6y agoI would disagree. There are a lot of data types which are a fixed length array of bytes (e.g. encryption keys), which make more sense as a value, rather than a container type.
- brundolf 6y agoMy impression may be biased by the types of projects I typically do. Pretty much the only time I ever use an array is for const data.