Where do suggestions go?

I have a suggestion concerning the Rust "book", I think the vec! macro could be better documented there, and also readers could be alerted to the danger of writing

vec![ 0, n ]

when what you need is

vec![ 0; n ]

This got me twice! Anyway, I am not clear where this should go on this site, or maybe it needs to go somewhere else altogether.

Why would you ever write vec![something; 0] to create and empty vector? Just write Vec::new() instead.

1 Like

" Why would you ever write vec![something; 0] to create and empty vector? Just write Vec::new() instead."

I have a vector that is counting how many times different bytes occur, so it has to be initialised to zero, and then the elements get incremented. Hash tables also have to be initialised.

Edit: oh, I wrote the example wrong, it should be vec![ 0, n ] and vec! [ 0; n ]. I will fix it.

Well then in that case it would be something like vec![0; 256] i guess. Also, in this case you can probably just use an array [0; 256] instead of a Vec and avoid the dynamic allocation.

Also, the syntax with semicolon is explained in the documentation of both the array primitive type (requiring Copy) and the vec! macro (requiring Clone).

I believe programmers are well trained to distinguish , from ;.

Anyway, Rust book is developed on GitHub, so you can simply open an isuue or PR here.

1 Like

I agree the library documentation explains it, but what I think needs mentioning in the "book" is to be careful to use ";" and not ',' when you intend ";". When you are learning Rust there is a lot to remember, and the use of ";" is novel for me. I got it right in one place, but then forgot the syntax - usually forgetting some syntax results in a compile error, but not in this case. I do have a bad memory, it has to be said!

At least in the past, I think it used to have better codegen. But maybe I'm thinking of vec![].

By the way, it might be possible to add a lint, maybe even warn-by-default one, against exact this syntax (vec![0, {number}]). If you really intend to use the two-valued vector, just add a second colon - vec![0, {number},], so that the typo is much less likely. It shouldn't warn in other cases, however (with other first elements), since this is, again, much less likely that someone would create a non-zero-uniformly-initialized Vec.

3 Likes