Skip to content

slice[slice.len()..] doesn't result in an index out of bounds error #32057

Closed
@petrochenkov

Description

@petrochenkov

.. ranges are [inclusive, exclusive) and index access is normally bounds checked, e.g. slice[slice.len()+100..] or slice[slice.len()] panics, however slice[slice.len()..]/slice[slice.len()..slice.len()] is an exception, it just returns an empty slice despite the first inclusive index being out of bounds.

I agree this is convenient and I guess this was implemented intentionally, and we can't silently break this now, but it at least needs to be 1) documented as intentional behavior, 2) tested as intentional behavior.
So far I haven't found neither documentation, nor tests, nor design rationale in the range indexing RFCs and their implementations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions