To iterate over words in a string in Rust, you can use the split_whitespace
method.
In this example,
str
with a value of "Hello World Rust"
.split_whitespace()
method on the string to split it into words.fn main() {
let str = "Hello World Rust";
for word in str.split_whitespace() {
println!("{}", word);
}
}
Hello World Rust
In this tutorial, we learned How to Iterate over Words in String in Rust language with well detailed examples.