rust-lang / rust-lang/rfcs

Non-error slicing

Open
#3,436 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

In Python and JavaScript, there's a function vec.slice that never throws an error:

vec.slice(x, -y)

means "remove the first x elements from the start and the first y elements from the end". Rust currently throws an error if x is greater than vec.len() - 1. It returns an empty vec when the span includes no elements: that's what you want for many use cases.

It's convenient to do Python/JavaScript like accesses as an idiomatic version of takeing and droping (in the Haskell sense) in Rust.

For clarity, here is an example implementation:

fn slice_vec<T>(vec: &[T], start: isize, end: isize) -> &[T] {
    let len = vec.len() as isize;
    let start = if start >= 0 { start } else { len + start };
    let end = if end >= 0 { end } else { len + end };
    let start = start.max(0) as usize;
    let end = end.min(len).max(start) as usize;
    &vec[start..end]
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the requested slicing semantics and example implementation in the issue, then review the Rust RFC process in this repository. Compare the proposed behavior with existing Rust slice, take, and drop APIs. Done means a decided RFC-level proposal that clearly specifies behavior for positive and negative bounds.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.