[bool; 128] and Vec<bool> w/ capaticy 128: both uses 16 bytes (ignoring header)?

For both [bool; 128] and Vec w/ capacity 128, ignoring the header, is it safe to assume that both takes 16 bytes ? If so, where is this documented ?

The core issue here is whether a bool is stored as a u8 or if Rust is somehow packing 8 bool's into 1 u8.

Bool is stored as u8, since we can address individual bools, but can't address anything less then byte.


bool takes one byte:

Boolean Type in The Rust Reference

An object with the boolean type has a size and alignment of 1 each.


If you need to store 128 booleans in 16 bytes, you can but you have to do some bitshifting to acecss the individual "booleans". Either you can implement that by hand, or you can use e.g. https://crates.io/crates/bitvec to do it for you.


@kaj : Agreed, using @H2CO3 's suggestion in Idiomatic way to implement u8 bit utils - #2 by H2CO3

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.