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.

2 Likes

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