Apart from managing the references, ownerships, allocations, and copies, we can also manage the memory layout of those structures we saw earlier, and we can do it by using both safe and unsafe code. Let's first understand how Rust manages the memory. Think of the following structure:
struct Complex { attr1: u8, attr2: u16, attr3: u8, }
When accessing the attributes from memory, they need to be aligned so that their position in memory is a multiple of their size, 16 bits in this case. That way, when we try to get each attribute, we will only need to add 16 bits to the base address of the structure, multiplied by the attribute. This makes information retrieval much more efficient, and it's done by the compiler automatically. The main issue with it is that for each attribute to be 16-bit aligned, the compiler would need to pad 8 bits for each of the first and third attributes.
This means that the structure gets converted to the following:
struct Complex...