Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Split string into iterator

fn main() {
    let text = String::from("mouse cat   oliphant");
    println!("{text}");

    let parts = text.split(' ');
    //println!("{:?}", parts);
    for part in parts {
        println!("{}", part);
    }
    println!("-------");

    let parts = text.split_whitespace();
    //println!("{:?}", parts);
    for part in parts {
        println!("{}", part);
    }

    //println!("{}", parts[0]); // cannot index into a value of type `SplitWhitespace<'_>`
}
mouse cat   oliphant
mouse
cat


oliphant
-------
mouse
cat
oliphant