Does Rust guarantee that Vec elements smaller than the word size are tightly packed?

How much space does the content take up Vec<u16>

? With, say, 1000 elements. Is there a way to test this with a test program?

Is it guaranteed how &[u16]

? (I think it would be pointless not to be since the conversion is cheap)

Also Vec[u8]

, &[u8]

etc.

( std::mem::size_of

returns the static size of this type, not its content)

+3


source to share


1 answer


Yes.

The vector content representation is the same as the slice representation, which is the same as the fixed size array representation. So you can compare std::mem::size_of::<[u16, 1]>()

and std::mem::size_of::<[u16, 10]>()

see that they always differ tenfold. ( Quote: This code .)



Rust uses byte indexing, so it u8

takes one byte, u16

takes two bytes, u32

takes four bytes, and takes u64

eight bytes. bool

also takes one byte; there are seven wasted bits per byte (hence types like volatile currently BitVec

).

+4


source







All Articles