How do I split a string in Rust?

アレックス picture アレックス · Oct 30, 2014 · Viewed 116.2k times · Source

From the documentation, it's not clear. In Java you could use the split method like so:

"some string 123 ffd".split("123");

Answer

Manishearth picture Manishearth · Oct 30, 2014

Use split()

let mut split = "some string 123 ffd".split("123");

This gives an iterator, which you can loop over, or collect() into a vector.

for s in split {
    println!("{}", s)
}
let vec = split.collect::<Vec<&str>>();
// OR
let vec: Vec<&str> = split.collect();