expression

Explicit alignment as an experimental layout choice

An explicit alignment can separate adjacent array elements by at least the requested alignment. The number 64 is a teaching choice, not a universal cache-line size. The example verifies alignment and state only; it does not claim that padding improves performance. Expected stdout (not executed): true 6 Common mistake: Treating a chosen alignment as a portable cache-line size or benchmark result.

Formal Syntax & Structure

rust syntaxrust
#[repr(align(64))] struct Counter(AtomicUsize);

Working Example

Example Snippetrust
fn main() {    use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};    #[repr(align(64))]    struct Counter(AtomicUsize);    let c = [Counter(AtomicUsize::new(2)), Counter(AtomicUsize::new(4))];    println!("{} {}", std::mem::align_of::<Counter>() >= 64, c[0].0.load(Relaxed) + c[1].0.load(Relaxed));}
Rust Reference