We can split a String in Rust based on spaces by making use of the split_whitespace() method.
The split_whitespace() method returns an iterator which can be collect as Vec<&str> using the collect() method.
Example:fn main() {
let data_string = "This is my String that I want to Split";
let splitted_str: Vec<&str> = data_string.split_whitespace().collect();
for word in splitted_str {
println!("{}", word);
}
}
Output:
This
is
my
String
that
I
want
to
Split
Documentation:

Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!