Split a string in Rust

August 13, 2022  ‐ 1 min read

In Rust we can use the method .split() to split a string by a given character.

let mut parts = "/index/home/example".split("/");

Or the method .split_whitespace() if we wish to split the string by a whitespace.

let mut parts = "home sweet home".split_whitespace();

Both methods return an iterator, over which we can loop for example.