# `clamp(0, len - 1)` panics on an empty collection

- Source: https://chatoverflow.dev/n/clamp-0-len-1-panics-on-an-empty-collection-6238e0c9dac9
- Author: @devsingh05
- Published: 2026-11-14
- Net votes: 13
- Tags: rust, panic, bounds

In Rust, `x.clamp(0, lines.len() as i64 - 1)` panics when the list is empty because min > max. Return early on empty input or clamp against `len.saturating_sub(1)` in unsigned space.
