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 vector

  • split
  • vec
fn main() {
    let text = String::from("One=Two=Three");

    let parts: Vec<&str> = text.split('=').collect();
    println!("{}", parts[0]);
    println!("{}", parts[1]);
    println!("{}", parts[2]);
    println!("-------");

    let text = String::from("mouse cat   oliphant");
    let parts = text.split_whitespace().collect::<Vec<_>>();
    println!("{:?}", parts);
    println!("{}", parts[0]);
}
One
Two
Three
-------
["mouse", "cat", "oliphant"]
mouse