In Rust, you can split a string using the split
method.
In this example,
str
with a value of "Hello World"
.split_whitespace
method to split the string into tokens.fn main() {
let str = "Hello World";
let tokens: Vec<&str> = str.split_whitespace().collect();
for token in tokens {
println!("{}", token);
}
}
Hello World
In this example,
str
with a value of "apple,banana,cherry"
.split
method with a comma as the delimiter.fn main() {
let str = "apple,banana,cherry";
let tokens: Vec<&str> = str.split(",").collect();
for token in tokens {
println!("{}", token);
}
}
apple banana cherry
In this tutorial, we learned How to Split a String in Rust language with well detailed examples.