How to split a string in Rust?
Strings in rust has built in methods, one of these methods issplit()
which takes another string as parameter, this parameter will be considered as delimiter, the return type of this method is an iterator, which you can loop over, or collect()
into a vector.Example of split a string in Rust
fn main() {
let mut splttedString = "Jhon,William,Robert,Joseph,Joshua".split(",");
for s in splttedString {
println!("{}", s)
}
}
Output
Jhon
William
Robert
Joseph
Joshua
William
Robert
Joseph
Joshua