Skip to content

RFC: Python-like slices in vec #1799

Closed
Closed
@nikomatsakis

Description

@nikomatsakis

I would like to modify the vector (and string, I suppose) slicing routines to be more "Python-like". This means that they are more lenient of invalid indices and they consider negative numbers to count from the right.

All in all I propose six functions:

vec::islice(v, from, to) // equivalent to v[from:to] in Python
vec::islice_from(v, from) // equivalent to v[from:] in Python, or vec::slice(v, from, vec::len(v))
vec::islice_to(v, to) // equivalent to v[:to] in Python, or vec::slice(v, 0, to)

vec::uslice(v, from, to)
vec::uslice_from(v, from, to)
vec::uslice_to(v, from, to)

The i family accepts signed integers: negative inputs are considered as counting from the right. The u family takes unsigned integers. All of them are tolerant of invalid indices, returning empty list rather than failing, as Python does. In my experience this is usually what you want.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-frontendArea: Compiler frontend (errors, parsing and HIR)C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions