Split vector of sentences to vector of words

concat split_whitespace

examples/vector-of-sentences-to-vector-of-words/src/main.rs

fn main() {}

fn split_to_strings(sentences: Vec<String>) -> Vec<String> {
    let vector_of_vectors = sentences
        .iter()
        .map(|sentence| {
            sentence
                .split_whitespace()
                .map(|word| word.to_owned())
                .collect::<Vec<String>>()
        })
        .collect::<Vec<Vec<String>>>();
    vector_of_vectors.concat()
}

#[test]
fn test_split() {
    let sentence = String::from("apple banana   peach  pear");
    let words = sentence.split_whitespace().collect::<Vec<&str>>();
    assert_eq!(words, vec!["apple", "banana", "peach", "pear"]);

    let words = sentence
        .split_whitespace()
        .map(|word| word.to_owned())
        .collect::<Vec<String>>();
    assert_eq!(words, vec!["apple", "banana", "peach", "pear"]);

    let vector_of_sentences = vec![
        String::from("apple banana   peach"),
        String::from("pear   cherry"),
    ];
    let words = split_to_strings(vector_of_sentences);
    assert_eq!(words, vec!["apple", "banana", "peach", "pear", "cherry"]);
}

Related Pages

Vectors in Rust

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo