Tricky split_whitespace()

I have to parse a string in a format as here:
"foo bar \"bar foo\" "easy task\""
and I expect to get ["foo", "bar", "bar foo", "easy task"]

Unfortunately split_whitespace() splits also by the internal spaces in a string entry.
In C++ I'd just use istream operator.

Is there a standard solution to that problem?

I don't know what you mean by standard solution, but you can implement this yourself using str::split with a variable keeping track whether we are in a quoted section of the string or not:

fn split(s: &str) -> Vec<String> {
    let mut wrapped = false;

    s.split(|c| {
        if c == '"' {
            wrapped = !wrapped;
        c == ' ' && !wrapped
    .map(|s| s.replace("\"", "")) // remove the quotation marks from the sub-strings

fn main() {
    let s = "foo bar \"bar foo\" \"easy task\"";
    let v = split(s);
    let expect: Vec<String> = ["foo", "bar", "bar foo", "easy task"]

    assert_eq!(v, expect);


Found this crate after quick search: split in shell_words - Rust. Is this perhaps what you want?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.