Skip to content

split_at family should return an Result #90410

Closed as not planned
Closed as not planned
@Stargateur

Description

@Stargateur

These function:

Are inconvenient to use, one need to check len (or be sure) then call split_at() that will also check len, first this can lead to bad asm #74938 & #30112. Then it's inelegant. This would be ok if these function would be unsafe, so not checking the len twice.

These function are better cause there return an Option:

I propose we add the equivalent of split_at returning a Result

impl slice {
  fn split_at_mid(&self, mid: usize) -> Result<(&[T], &[T]), usize>;
  fn split_at_mid_mut(&mut self, mid: usize) -> Result<(&mut [T], &mut [T]), usize>;
}

impl str {
  fn split_at_mid(&self, mid: usize) -> Result<(&str, &str), usize>;
  fn split_at_mid_mut(&mut self, mid: usize) -> Result<(&mut str, &mut str), usize>;
}

The Err be mid or len (but we could imagine more precise error for example str could indicate char error boundary) or we keep it simple with Option

BTW: We miss split_first() and split_last() on str.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions