Skip to content

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

Open
@crumblingstatue

Description

@crumblingstatue

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-acceptedCategory: A feature request that has been accepted pending implementation.T-libs-apiRelevant to the library API 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