My intent was to initialize a vector of size 3:
let mut cnts = vec![0usize, 3];
The code compiled with no problem, but when I ran it I got this message:
thread 'main' panicked at 'index out of bounds: the len is 2 but the index is 2', /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libcore/slice/mod.rs:2701:14 note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
WTF! It compiled, now what. So I started put print statements in the code to find out where it bombed. An hour later, I determined
cnts was creating a vector of
[0, 3] instead of
[0, 0, 0].
vec![0usize, 3] it should have been
vec![0size; 3] – a semicolon vs a coma.
I am sure this mistake has happened more than once to people and it would be really nice for the compiler to throw a warning message:
Hey, you wrote vec![x, y] did you really mean vec![x; y]?
A better delimiter character would probably eliminate most of these errors, but I guess it’s too late in the game to introduce that (or is it?).