Skip to content

str::split and str::splitn doc unclear as to how empty string input is handled #14222

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

Closed
pnkfelix opened this issue May 15, 2014 · 0 comments · Fixed by #14335
Closed

str::split and str::splitn doc unclear as to how empty string input is handled #14222

pnkfelix opened this issue May 15, 2014 · 0 comments · Fixed by #14335

Comments

@pnkfelix
Copy link
Member

Here's the doc for str::split and str::splitn:

http://static.rust-lang.org/doc/master/core/str/trait.StrSlice.html#tymethod.split

Pop quiz: what does this program print. In particular, what will the first call to next() return when we are doing a split over the empty string? (Also, should it do something else? But that's not as important to me as making the doc clear.)

fn take_three<'a, I:Iterator<&'a str>>(input: &str, variant: &str, mut i: I) {
    let fst = i.next();
    let snd = if fst.is_some() { i.next() } else { None };
    let thd = if snd.is_some() { i.next() } else { None };
    println!("{:8} {:3}: {} {} {}",
             format!("`{}`", input), variant, fst, snd, thd);
}

fn main() {
    let i = "a.b";
    take_three(i,   "", i.split('.'));

    let i = ".";
    take_three(i,   "", i.split('.'));

    let i = "";
    take_three(i,   "", i.split('.'));

    let i = "a.b";
    take_three(i, "n1", i.splitn('.', 1));

    let i = ".";
    take_three(i, "n1", i.splitn('.', 1));

    let i = "";
    take_three(i, "n1", i.splitn('.', 1));

    let i = "a.b";
    take_three(i, "n0", i.splitn('.', 0));

    let i = ".";
    take_three(i, "n0", i.splitn('.', 0));

    let i = "";
    take_three(i, "n0", i.splitn('.', 0));
}

(To be fair: this is not one of those trick questions where splitn's behavior deviates from split on the empty string. If you have the correct intuition about how one behaves, then you'll have the right intuition for the other. But I didn't have an intuition in either direction, which led me to first consult the doc.)

@huonw huonw added the A-docs label May 15, 2014
tbu- added a commit to tbu-/rust that referenced this issue May 21, 2014
In particular, show examples for splitting the empty string and using `splitn`
with a count of 0.

Fix rust-lang#14222.
bors added a commit that referenced this issue May 22, 2014
In particular, show examples for splitting the empty string and using `splitn`
with a count of 0.

Fix #14222.
lnicola pushed a commit to lnicola/rust that referenced this issue Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants