rayon-rs/rayon · error

range start should be <= length

Error message

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

What it means

`simplify_range` normalizes any RangeBounds to a Range and panics when the start bound exceeds the collection length `len`. The library requires start <= len (Included) or start < len (Excluded); anything else is an out-of-range slice argument.

Solutions

  1. Clamp the range start to the collection length before the call
  2. Use `start.min(len)` when constructing the range
  3. Check `len` first and skip the drain when start >= len
  4. Guard Excluded starts so start+1 <= len

Example fix

// before
vec.par_drain(start..end);
// after
let start = start.min(vec.len());
let end = end.min(vec.len());
vec.par_drain(start..end);
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

fn in_bounds(i: usize, len: usize) -> bool { i <= len }

Prevention

When it happens

Trigger: Calling a draining/splice-style API (e.g. `drain(n..)` on a collection of length < n, or `drain(m..)` with m > len), or an Excluded start equal to len (e.g. `drain((len-0..).start_bound_excluded)` patterns like `range start (len+1)`).

Common situations: Computing a drain start from user input or a computed offset that wasn't clamped to the collection length; off-by-one after removing items before draining.

Related errors


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

Appendix: source

Thrown at src/math.rs:9

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)