I am creating a lot of structs using Option<Vec<T>>
instead of Vec<T>
to not allocate memory when not needed. And I looked up this answer about the sizes of both Vec
& Option<Vec>>
and both were same.
So my question is this. Should I use Vec<T>
and check its .len()
for the same effect or continue using Option
? Or are they equivalent?
Also originally I am a JavaScript Developer. And getting paranoid over to allocate or not to allocate.
An empty Vec
doesn't allocate, because there's nothing for it to store.
The two are the same size because of an optimisation the compiler uses, but this has no bearing on what the types mean. Even with this optimisation, an empty Vec
and None
aren't the same thing. One indicates the presence of an empty Vec
, the other indicates the absence of any Vec
at all. Depending on what exactly you're doing, the distinction could be important.
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