These are chat archives for rust-lang/rust

19th
Jul 2016
Andrew De Ponte
@cyphactor
Jul 19 2016 03:32
Anyone know the best way to create a two dimensional array of size determined at runtime in Rust?
Peter Atashian
@retep998
Jul 19 2016 06:09
@cyphactor Create a Vec<T> and index it via foo[y*width + x]
Andrew De Ponte
@cyphactor
Jul 19 2016 06:11
thx @retep998
LeonineKing1199
@LeonineKing1199
Jul 19 2016 15:20
I know it's "hackish" but shouldn't Vec<Vec<T>> work?
Thomas Koehler
@Bastacyclop
Jul 19 2016 15:22
@LeonineKing1199 I personally don't think it has anything "hackish", the difference is that you need more allocations and you get less contiguity. But it does have other advantages.
LeonineKing1199
@LeonineKing1199
Jul 19 2016 15:23
One advantage I like is that it's indexable by [x][y] instead of the 1d way.
Thomas Koehler
@Bastacyclop
Jul 19 2016 15:24
I wouldn't consider that an advantage
LeonineKing1199
@LeonineKing1199
Jul 19 2016 15:26
Really? Why not?
Thomas Koehler
@Bastacyclop
Jul 19 2016 15:26
You can use a get method and probably do the same thing with Index operator overload
LeonineKing1199
@LeonineKing1199
Jul 19 2016 15:26
Well, that's a matter of opinion. I think it reflects mathematical notation to use data[x][y]
Thomas Koehler
@Bastacyclop
Jul 19 2016 15:28
Also, since you usually iterate over y's before x's, you should do data[y][x] for better cache performance
Elia Schelokov
@thaumant
Jul 19 2016 15:30
Why vec and not array? I thought arrays are exactly for creating containers with size determined at compile time.
Thomas Koehler
@Bastacyclop
Jul 19 2016 15:30
"Anyone know the best way to create a two dimensional array of size determined at runtime in Rust?"
Elia Schelokov
@thaumant
Jul 19 2016 15:31
Misread that : )
LeonineKing1199
@LeonineKing1199
Jul 19 2016 15:37
Well, cache performance relates to many things and not just how you arrange the data.
For one, memory access is equally important.
And even then, I wouldn't worry until you run it through cachegrind.
Thomas Koehler
@Bastacyclop
Jul 19 2016 15:37
I'm not saying it's always better
LeonineKing1199
@LeonineKing1199
Jul 19 2016 15:37
Wait, do Rust binaries do that?
Can they?
Like, can I valgrind Rust code?
Thomas Koehler
@Bastacyclop
Jul 19 2016 15:37
I think yes
LeonineKing1199
@LeonineKing1199
Jul 19 2016 15:38
Caches are pretty big nowadays so you might be able to get away with a lot :P
Better cpus allow for worse programmers to program
And I'm not excluding myself from that group
Thomas Koehler
@Bastacyclop
Jul 19 2016 15:51
you can do stuff like that
x)