External Publication
Visit Post

Introduce a way to construct Range from start + length

Rust Internals [Unofficial] March 28, 2026
Source

The start .. start + len syntax is annoying indeed, especially when there's a non-trivial calculation for the start, or just a very long method name.

[start..][..len] is okay when the bounds checks get merged and optimized out, but otherwise it leaves two panicking branches in the compiled code (since each panic call has to get its unique argument). .get(start..)?.get(..len)? optimizes fine, but that's a lot of punctuation.

This could be added as a method to RangeFrom without being blocked by a new syntax:

(start..).take(len)

I wouldn't mind a requirement for the types to be Copy or integer-ish. For ranges of string or other complex keys in something like BTreeMap, a "length" may not make sense anyway.

I wouldn't want it to panic any more than start + len does, because it would ruin uses with .get(start ..+ len)?. .get() already checks for the start <= len, but a panic in ..+ would create a redundant check that couldn't be merged together with the one in .get().

.get(..) is used specifically to avoid panics from [..], so having another possible panic inserted by ..+ would be an unpleasant surprise.


Vorpal:

you really should be writing 'a'..'{', which is not ideal for human readers

It seems like there could be plenty more convenience methods, e.g. ('a'..='z').to_exclusive()?

Discussion in the ATmosphere

Loading comments...