What is align attribute use

  1. When to use align attribute
  2. How it works

Explain with little basic example

It's only ever necessary for unsafe code.

If you don't already know you need it, you can just not use it.


But i want to know about that feature

It sets alignment of the type. Sometimes you need to control alignment of types precisely for cases like SIMD operations, cacheline padding, memory page handling and MMIO.

The align attribute is for setting your type's alignment and is typically only necessary when doing things with raw pointers. SIMD also has certain requirements where loading unaligned data will trigger a hardware fault.

The best source is the corresponding section from The Rust Reference.

From there, knowing how to use alignment is the same regardless of which language you use.

1 Like

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.