Although it is possible to write a closure to split on arbitrarily complex conditions, it easier to use regex crate for splitting based on regular expressions.
Another approach without using a closure would be to make use of string slice patterns. As an example, we can write the above split as,
Splits can be collected to a vector and other types of Rust collections with collect method.
How to split a string based on regular expressions?
Splitting a string such as "hello there; how| are|you, chathura" based on multiple characters and taking into account multiple spaces is not simple without using regular expressions.
Following solution demonstrates the use of regex crate to accomplish the task.
for s in message.split(&['|', ';'][..]) {
// ...
}
fn split_by_regex() {
let message = "hello there; how| are|you, Chathura";
let re = regex::Regex::new(r"[;,|\s]\s*").unwrap();
for s in re.split(message) {
dbg!(s);
}
}