Split vector of sentences to vector of words

concat split_whitespace


fn main() {}

fn split_to_strings(sentences: Vec<String>) -> Vec<String> {
    let vector_of_vectors = sentences
        .map(|sentence| {
                .map(|word| word.to_owned())

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
        .map(|word| word.to_owned())
    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


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