emilk/egui · error

Vec2b index out of bounds: {index}

Error message

Vec2b index out of bounds: {index}

What it means

`Vec2b` (a 2-component boolean vector) implements `Index<usize>` with only 0 (x) and 1 (y) valid; any other index panics with this message. The panic enforces the 2D invariant of the type at the indexing site.

Source

Thrown at crates/emath/src/vec2b.rs:78

    }
}

impl From<[bool; 2]> for Vec2b {
    #[inline]
    fn from([x, y]: [bool; 2]) -> Self {
        Self { x, y }
    }
}

impl core::ops::Index<usize> for Vec2b {
    type Output = bool;

    #[inline(always)]
    fn index(&self, index: usize) -> &bool {
        match index {
            0 => &self.x,
            1 => &self.y,
            _ => panic!("Vec2b index out of bounds: {index}"),
        }
    }
}

impl core::ops::IndexMut<usize> for Vec2b {
    #[inline(always)]
    fn index_mut(&mut self, index: usize) -> &mut bool {
        match index {
            0 => &mut self.x,
            1 => &mut self.y,
            _ => panic!("Vec2b index out of bounds: {index}"),
        }
    }
}

impl core::ops::Not for Vec2b {
    type Output = Self;

View on GitHub (pinned to 441971a776)

Solutions

  1. Only use indices 0 and 1; check the index before indexing.
  2. Use the named accessors `vec2b.x` / `vec2b.y` when the component is static.
  3. Iterate with `for axis in 0..2` or convert with `vec2b.to_array()` and index the `[bool; 2]`.
  4. If more flags are needed, use a fixed-size array or a 3-component type instead of Vec2b.

Example fix

// before
let flag = vec2b[axis]; // axis may be 2+
// after
let flag = if axis == 0 { vec2b.x } else if axis == 1 { vec2b.y } else { fallback };
Defensive patterns

Strategy: type-guard

Validate before calling

fn vec2b_get(v: emath::Vec2b, index: usize) -> Option<bool> {
    match index { 0 => Some(v.x), 1 => Some(v.y), _ => None }
}

Type guard

fn is_vec2b_index(i: usize) -> bool { i < 2 }

Try / catch

let flag = std::panic::catch_unwind(|| v[index]).unwrap_or(false); // prefer index check

Prevention

When it happens

Trigger: Reading `vec2b[2]` or beyond, usually via a dimension loop that assumes 3 or more axes, or an index computed from external input (config, user selection) applied unchecked.

Common situations: Generic layout code iterating axes over the wrong bound; porting from BoolVec3-like types; dynamically chosen axis stored as usize in UI state and later used to index the flag vector.

Related errors


AI-assisted analysis of emilk/egui@441971a776 (2026-09-12). Data as JSON: /api/errors/2f25a354f5ee561b. Report an issue: GitHub.