Entities and Components

Generational indices, and why "just use an integer id" breaks.

Part 1 of 3Updated

An entity is an id. The naive version is a usize, and it breaks the moment you delete an entity and reuse the slot: an old handle now silently points at a different entity.

The fix is a generational index:

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Entity {
    index: u32,
    generation: u32,
}

Each slot has a generation counter that increments on delete. A stale handle carries the old generation, so lookups return None instead of the wrong data. This pattern shows up everywhere in Rust β€” it’s how you get graph-like references without Rc or unsafe.