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

Add as_str method for str::Split(Whitespace) #43090

Open
crumblingstatue opened this issue Jul 6, 2017 · 5 comments
Open

Add as_str method for str::Split(Whitespace) #43090

crumblingstatue opened this issue Jul 6, 2017 · 5 comments
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@crumblingstatue
Copy link
Contributor

Consider the following use case:

// A command interpreter with a "print" command, which prints what comes after it
let mut words = line.split_whitespace();

match words.next() {
    "print" => {
        // How could we get the rest that comes after the command word?
        // We could perhaps collect the words, and join them with a space, but that's lossy,
        // and doesn't sound very efficient.
        let rest;
        println!("{}", rest),
    }
    unk => println!("Unknown command: {}", unk),
}

Some iterators, like str::Chars already have an as_str method. The only question is whether it is possible to implement as_str for the Split API, without making any breaking changes.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 6, 2017

#34204 has a previous attempt and some discussion

@crumblingstatue
Copy link
Contributor Author

Meanwhile, I implemented an alternative SplitWhitespace, because I frequently make use of this feature.

@Mark-Simulacrum Mark-Simulacrum added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Jul 19, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 28, 2017
@dtolnay dtolnay added C-feature-accepted Category: A feature request that has been accepted pending implementation. and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Nov 18, 2017
@dtolnay
Copy link
Member

dtolnay commented Nov 18, 2017

I would be open to adding this for str's split iterators, as long as it does not require all Searchers to support as_str which isn't possible because of #34204 (comment).

@Kixunil
Copy link
Contributor

Kixunil commented Feb 14, 2020

Do I understand correctly that it's impossible just for reverse iterator? In other words, implementing this for forward iterator should be fine, right?

If yes, then maybe we could implement just that, as I expect that case to be most common, so it's already useful. I'm willing to do PR.

@crumblingstatue
Copy link
Contributor Author

crumblingstatue commented Oct 23, 2020

Split now has it thanks to #75265, but SplitWhitespace still doesn't, so I'm not closing this issue yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-accepted Category: A feature request that has been accepted pending implementation. 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