rust read splited string as vector
To read a split string as a vector in Rust, you can use the split_whitespace
method to split the string into individual words, and then collect them into a vector. Here's an example:
fn main() {
let input = "Hello world! Rust is awesome.";
let words: Vec<&str> = input.split_whitespace().collect();
println!("{:?}", words);
}
In this code, the split_whitespace
method splits the input string into words based on whitespace. The resulting iterator is then collected into a vector using the collect
method. Finally, the vector is printed using println!
.
When you run this code, the output will be:
["Hello", "world!", "Rust", "is", "awesome."]
This is the vector representation of the split string. Each word is an element in the vector.