I'm re-implementing a hashtable in Rust. Eventually I'd like to make this distributed and have quorums, but for now it's a hashtable on a single system.
I want the size of the table to be passed in as an argument so the table should be dynamically sized. I don't want the table to grow because that will mess with my hash function which works off a modulo operation. I see a couple of options:
Vec
s and arrays, so I'll need a Vec
/array anyways?In C++, I could have just used a dynamically sized array. What's the best choice here?
A boxed slice is dynamically sized, the length cannot be changed once created, and it owns the contained data:
let the_vec: Vec<i32> = vec![1, 2, 3];
let the_boxed_slice: Box<[i32]> = the_vec.into_boxed_slice();
The types aren't required here, they are just present for pedagogical reasons.
However, it's dubious whether you will get any performance benefit. A Vec
is three pointer sized values (data, size, capacity). A Box<[T]>
is only two: data and size. The overhead of having the extra value is minuscule in most cases.
The main benefit is to be statically guaranteed that the size won't change; you won't statically know that it's a certain size. Such a guarantee might happen if type-level numbers ever happen.
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With