Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

&str should have split_first #48731

Closed
SoniEx2 opened this issue Mar 4, 2018 · 7 comments
Closed

&str should have split_first #48731

SoniEx2 opened this issue Mar 4, 2018 · 7 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@SoniEx2
Copy link
Contributor

SoniEx2 commented Mar 4, 2018

string slices should have a split_first, just like array slices.

returns -> (char, &str)

@frewsxcv frewsxcv added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 4, 2018
@kennytm
Copy link
Member

kennytm commented Mar 4, 2018

This function was called slice_shift_char but it was removed because you could do this instead:

fn slice_shift_char(a: &str) -> Option<(char, &str)> {
    let mut chars = a.chars();
    chars.next().map(|c| (c, chars.as_str()))
}

fn main() {
    assert_eq!(slice_shift_char("hello"), Some(('h', "ello")));
    assert_eq!(slice_shift_char("ĺḿńóṕ"), Some(('ĺ', "ḿńóṕ")));
    assert_eq!(slice_shift_char(""), None);
}

@steveklabnik
Copy link
Member

We don't keep bugs open for small feature requests for libraries like this; big things need an RFC, small things can be PR'd. Thanks!

@Stargateur
Copy link
Contributor

Stargateur commented Aug 18, 2021

This function was called slice_shift_char but it was removed because you could do this instead:

fn slice_shift_char(a: &str) -> Option<(char, &str)> {
    let mut chars = a.chars();
    chars.next().map(|c| (c, chars.as_str()))
}

fn main() {
    assert_eq!(slice_shift_char("hello"), Some(('h', "ello")));
    assert_eq!(slice_shift_char("ĺḿńóṕ"), Some(('ĺ', "ḿńóṕ")));
    assert_eq!(slice_shift_char(""), None);
}

I don't feel it's a good reason. The same reasoning could be say about slice.

Also, this is far from obvious.

@kennytm
Copy link
Member

kennytm commented Aug 19, 2021

There isn't a slice method in str

@Stargateur
Copy link
Contributor

@kennytm One could say the same for slice split_first():

fn slice_shift_slice(a: &[u8]) -> Option<(u8, &[u8])> {
    let mut iter = a.iter();
    iter.next().map(|&n| (n, iter.as_slice()))
}

@kennytm
Copy link
Member

kennytm commented Aug 20, 2021

slice::split_first has been stabilized 1.5.0. We can't retroactively destabilize a std method now even if better alternative exists.

fn split_first<T>(a: &[T]) -> Option<(&T, &[T])> {
    match a {
        [head, tail @ ..] => Some((head, tail)),
        [] => None,
    }
}

Meanwhile str::slice_shift_char was considered redundant before 1.0.0 was released, so we could remove it without the stabilization guarantee.

I was just explaining the decision made  2  5 years ago. If you would like to bring back str::split_first_char, quoting Steve above,

small things can be PR'd. Thanks!

@Stargateur
Copy link
Contributor

I know all that, I just explain your argument could work the same for have refused split_first().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants