rayon-rs/rayon · error

range end should be <= length

Error message

range end {bound:?} should be <= length {len}

What it means

`simplify_range` normalizes any RangeBounds to a Range and panics when the end bound exceeds the collection length `len`. Valid ends are Unbounded, Excluded(i<=len), or Included(i<len); otherwise the slice end is out of range.

Solutions

  1. Clamp the end with `end.min(len)` before constructing the range
  2. Use `..len` not `..=len` for a full-range drain
  3. Fetch `len` from the same collection being drained, immediately before the call

Example fix

// before
vec.par_drain(..=vec.len()); // Included(len) panics when len>0
// after
vec.par_drain(..vec.len()); // or vec.par_drain(..);
Defensive patterns

Strategy: validation

Validate before calling

fn valid_drain_end(range: std::ops::Range<usize>, len: usize) -> bool {
    range.end <= len
}

Type guard

fn end_in_bounds(e: usize, len: usize) -> bool { e <= len }

Prevention

When it happens

Trigger: Calling a drain-style API with an end bound past the collection length, e.g. `drain(..len+1)` or `drain(..=len)` on a collection of exactly `len` items.

Common situations: Hardcoding an end index computed from a stale/other collection's length; off-by-one when using inclusive ranges (`..=len` instead of `..len`).

Related errors


AI-assisted analysis of rayon-rs/rayon@ee0a00bdb1 (2026-09-07). Data as JSON: /api/errors/546f609a9b63a587. Report an issue: GitHub.

Appendix: source

Thrown at src/math.rs:15

use std::ops::{Bound, Range, RangeBounds};

/// Normalize arbitrary `RangeBounds` to a `Range`
pub(super) fn simplify_range(range: impl RangeBounds<usize>, len: usize) -> Range<usize> {
    let start = match range.start_bound() {
        Bound::Unbounded => 0,
        Bound::Included(&i) if i <= len => i,
        Bound::Excluded(&i) if i < len => i + 1,
        bound => panic!("range start {bound:?} should be <= length {len}"),
    };
    let end = match range.end_bound() {
        Bound::Unbounded => len,
        Bound::Excluded(&i) if i <= len => i,
        Bound::Included(&i) if i < len => i + 1,
        bound => panic!("range end {bound:?} should be <= length {len}"),
    };
    if start > end {
        panic!(
            "range start {:?} should be <= range end {:?}",
            range.start_bound(),
            range.end_bound()
        );
    }
    start..end
}

View on GitHub (pinned to ee0a00bdb1)